blob: d396137fa319e2545c65f837e1f9c3974ef23890 [file] [log] [blame]
Chris Lattnerf2836d12007-03-31 04:06:36 +00001//===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf2836d12007-03-31 04:06:36 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass munges the code in the input function to better prepare it for
Gordon Henriksen829046b2008-05-08 17:46:35 +000011// SelectionDAG-based code generation. This works around limitations in it's
12// basic-block-at-a-time approach. It should eventually be removed.
Chris Lattnerf2836d12007-03-31 04:06:36 +000013//
14//===----------------------------------------------------------------------===//
15
Quentin Colombeta3490842014-02-22 00:07:45 +000016#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Analysis/InstructionSimplify.h"
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +000021#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000022#include "llvm/Analysis/TargetLibraryInfo.h"
Quentin Colombetc32615d2014-10-31 17:52:53 +000023#include "llvm/Analysis/TargetTransformInfo.h"
Sanjay Patel69a50a12015-10-19 21:59:12 +000024#include "llvm/Analysis/ValueTracking.h"
Petar Jovanovic644b8c12016-04-13 12:25:25 +000025#include "llvm/Analysis/MemoryBuiltins.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000026#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000030#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Function.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000032#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/IRBuilder.h"
34#include "llvm/IR/InlineAsm.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +000037#include "llvm/IR/MDBuilder.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000038#include "llvm/IR/PatternMatch.h"
Ramkumar Ramachandradba73292015-01-14 23:27:07 +000039#include "llvm/IR/Statepoint.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000040#include "llvm/IR/ValueHandle.h"
Chandler Carrutha4ea2692014-03-04 11:26:31 +000041#include "llvm/IR/ValueMap.h"
Chris Lattnerf2836d12007-03-31 04:06:36 +000042#include "llvm/Pass.h"
Sanjay Pateld66607b2016-04-26 17:11:17 +000043#include "llvm/Support/BranchProbability.h"
Evan Cheng8b637b12010-08-17 01:34:49 +000044#include "llvm/Support/CommandLine.h"
Evan Chengd3d80172007-12-05 23:58:20 +000045#include "llvm/Support/Debug.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000046#include "llvm/Support/raw_ostream.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000047#include "llvm/Target/TargetLowering.h"
Hal Finkelc3998302014-04-12 00:59:48 +000048#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000049#include "llvm/Transforms/Utils/BasicBlockUtils.h"
50#include "llvm/Transforms/Utils/BuildLibCalls.h"
Preston Gurdcdf540d2012-09-04 18:22:17 +000051#include "llvm/Transforms/Utils/BypassSlowDivision.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000052#include "llvm/Transforms/Utils/Local.h"
Ahmed Bougachae03bef72015-01-12 17:22:43 +000053#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Chris Lattnerf2836d12007-03-31 04:06:36 +000054using namespace llvm;
Chris Lattnerd616ef52008-11-25 04:42:10 +000055using namespace llvm::PatternMatch;
Chris Lattnerf2836d12007-03-31 04:06:36 +000056
Chandler Carruth1b9dde02014-04-22 02:02:50 +000057#define DEBUG_TYPE "codegenprepare"
58
Cameron Zwarichced753f2011-01-05 17:27:27 +000059STATISTIC(NumBlocksElim, "Number of blocks eliminated");
Evan Cheng0663f232011-03-21 01:19:09 +000060STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
61STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
Cameron Zwarichced753f2011-01-05 17:27:27 +000062STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
63 "sunken Cmps");
64STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
65 "of sunken Casts");
66STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
67 "computations were sunk");
Evan Cheng0663f232011-03-21 01:19:09 +000068STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
69STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
Geoff Berry5256fca2015-11-20 22:34:39 +000070STATISTIC(NumAndsAdded,
71 "Number of and mask instructions added to form ext loads");
72STATISTIC(NumAndUses, "Number of uses of and mask instructions optimized");
Evan Cheng0663f232011-03-21 01:19:09 +000073STATISTIC(NumRetsDup, "Number of return instructions duplicated");
Devang Patel53771ba2011-08-18 00:50:51 +000074STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved");
Benjamin Kramer047d7ca2012-05-05 12:49:22 +000075STATISTIC(NumSelectsExpanded, "Number of selects turned into branches");
Tim Northovercea0abb2014-03-29 08:22:29 +000076STATISTIC(NumAndCmpsMoved, "Number of and/cmp's pushed into branches");
Quentin Colombetc32615d2014-10-31 17:52:53 +000077STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed");
Jakob Stoklund Oleseneb12f492010-09-30 20:51:52 +000078
Cameron Zwarich338d3622011-03-11 21:52:04 +000079static cl::opt<bool> DisableBranchOpts(
80 "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
81 cl::desc("Disable branch optimizations in CodeGenPrepare"));
82
Ramkumar Ramachandradba73292015-01-14 23:27:07 +000083static cl::opt<bool>
84 DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false),
85 cl::desc("Disable GC optimizations in CodeGenPrepare"));
86
Benjamin Kramer3d38c172012-05-06 14:25:16 +000087static cl::opt<bool> DisableSelectToBranch(
88 "disable-cgp-select2branch", cl::Hidden, cl::init(false),
89 cl::desc("Disable select to branch conversion."));
Benjamin Kramer047d7ca2012-05-05 12:49:22 +000090
Hal Finkelc3998302014-04-12 00:59:48 +000091static cl::opt<bool> AddrSinkUsingGEPs(
92 "addr-sink-using-gep", cl::Hidden, cl::init(false),
93 cl::desc("Address sinking in CGP using GEPs."));
94
Tim Northovercea0abb2014-03-29 08:22:29 +000095static cl::opt<bool> EnableAndCmpSinking(
96 "enable-andcmp-sinking", cl::Hidden, cl::init(true),
97 cl::desc("Enable sinkinig and/cmp into branches."));
98
Quentin Colombetc32615d2014-10-31 17:52:53 +000099static cl::opt<bool> DisableStoreExtract(
100 "disable-cgp-store-extract", cl::Hidden, cl::init(false),
101 cl::desc("Disable store(extract) optimizations in CodeGenPrepare"));
102
103static cl::opt<bool> StressStoreExtract(
104 "stress-cgp-store-extract", cl::Hidden, cl::init(false),
105 cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"));
106
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000107static cl::opt<bool> DisableExtLdPromotion(
108 "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
109 cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in "
110 "CodeGenPrepare"));
111
112static cl::opt<bool> StressExtLdPromotion(
113 "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
114 cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) "
115 "optimization in CodeGenPrepare"));
116
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000117static cl::opt<bool> DisablePreheaderProtect(
118 "disable-preheader-prot", cl::Hidden, cl::init(false),
119 cl::desc("Disable protection against removing loop preheaders"));
120
Eric Christopherc1ea1492008-09-24 05:32:41 +0000121namespace {
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000122typedef SmallPtrSet<Instruction *, 16> SetOfInstrs;
Benjamin Kramer4cd5faa2015-07-31 17:00:39 +0000123typedef PointerIntPair<Type *, 1, bool> TypeIsSExt;
Quentin Colombetf5485bb2014-11-13 01:44:51 +0000124typedef DenseMap<Instruction *, TypeIsSExt> InstrToOrigTy;
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000125class TypePromotionTransaction;
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000126
Chris Lattner2dd09db2009-09-02 06:11:42 +0000127 class CodeGenPrepare : public FunctionPass {
Bill Wendling7a639ea2013-06-19 21:07:11 +0000128 const TargetMachine *TM;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000129 const TargetLowering *TLI;
Quentin Colombetc32615d2014-10-31 17:52:53 +0000130 const TargetTransformInfo *TTI;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000131 const TargetLibraryInfo *TLInfo;
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000132 const LoopInfo *LI;
Nadav Rotem465834c2012-07-24 10:51:42 +0000133
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000134 /// As we scan instructions optimizing them, this is the next instruction
135 /// to optimize. Transforms that can invalidate this should update it.
Chris Lattner7a277142011-01-15 07:14:54 +0000136 BasicBlock::iterator CurInstIterator;
Evan Cheng3b3de7c2008-12-19 18:03:11 +0000137
Evan Cheng0663f232011-03-21 01:19:09 +0000138 /// Keeps track of non-local addresses that have been sunk into a block.
139 /// This allows us to avoid inserting duplicate code for blocks with
140 /// multiple load/stores of the same address.
Nick Lewycky5fb19632013-05-08 09:00:10 +0000141 ValueMap<Value*, Value*> SunkAddrs;
Cameron Zwarichce3b9302011-01-06 00:42:50 +0000142
Ahmed Bougachaf3299142015-06-17 20:44:32 +0000143 /// Keeps track of all instructions inserted for the current function.
144 SetOfInstrs InsertedInsts;
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000145 /// Keeps track of the type of the related instruction before their
146 /// promotion for the current function.
147 InstrToOrigTy PromotedInsts;
148
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000149 /// True if CFG is modified in any way.
Devang Patel8f606d72011-03-24 15:35:25 +0000150 bool ModifiedDT;
Evan Cheng0663f232011-03-21 01:19:09 +0000151
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000152 /// True if optimizing for size.
Benjamin Kramer047d7ca2012-05-05 12:49:22 +0000153 bool OptSize;
154
Mehdi Amini4fe37982015-07-07 18:45:17 +0000155 /// DataLayout for the Function being processed.
156 const DataLayout *DL;
157
Chris Lattnerf2836d12007-03-31 04:06:36 +0000158 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000159 static char ID; // Pass identification, replacement for typeid
Craig Topperc0196b12014-04-14 00:51:57 +0000160 explicit CodeGenPrepare(const TargetMachine *TM = nullptr)
Mehdi Amini4fe37982015-07-07 18:45:17 +0000161 : FunctionPass(ID), TM(TM), TLI(nullptr), TTI(nullptr), DL(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000162 initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
163 }
Craig Topper4584cd52014-03-07 09:26:03 +0000164 bool runOnFunction(Function &F) override;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000165
Craig Topper4584cd52014-03-07 09:26:03 +0000166 const char *getPassName() const override { return "CodeGen Prepare"; }
Evan Cheng99cafb12012-12-21 01:48:14 +0000167
Craig Topper4584cd52014-03-07 09:26:03 +0000168 void getAnalysisUsage(AnalysisUsage &AU) const override {
George Burgess IVd4febd12016-03-22 21:25:08 +0000169 // FIXME: When we can selectively preserve passes, preserve the domtree.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000170 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth705b1852015-01-31 03:43:40 +0000171 AU.addRequired<TargetTransformInfoWrapperPass>();
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000172 AU.addRequired<LoopInfoWrapperPass>();
Andreas Neustifterf8cb7582009-09-16 09:26:52 +0000173 }
174
Chris Lattnerf2836d12007-03-31 04:06:36 +0000175 private:
Sanjay Patelfc580a62015-09-21 23:03:16 +0000176 bool eliminateFallThrough(Function &F);
177 bool eliminateMostlyEmptyBlocks(Function &F);
178 bool canMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
179 void eliminateMostlyEmptyBlock(BasicBlock *BB);
180 bool optimizeBlock(BasicBlock &BB, bool& ModifiedDT);
181 bool optimizeInst(Instruction *I, bool& ModifiedDT);
182 bool optimizeMemoryInst(Instruction *I, Value *Addr,
Matt Arsenaultf72b49b2015-06-04 16:17:38 +0000183 Type *AccessTy, unsigned AS);
Sanjay Patelfc580a62015-09-21 23:03:16 +0000184 bool optimizeInlineAsmInst(CallInst *CS);
185 bool optimizeCallInst(CallInst *CI, bool& ModifiedDT);
186 bool moveExtToFormExtLoad(Instruction *&I);
187 bool optimizeExtUses(Instruction *I);
Geoff Berry5256fca2015-11-20 22:34:39 +0000188 bool optimizeLoadExt(LoadInst *I);
Sanjay Patelfc580a62015-09-21 23:03:16 +0000189 bool optimizeSelectInst(SelectInst *SI);
190 bool optimizeShuffleVectorInst(ShuffleVectorInst *SI);
Sanjay Patel0ed9aea2015-11-02 23:22:49 +0000191 bool optimizeSwitchInst(SwitchInst *CI);
Sanjay Patelfc580a62015-09-21 23:03:16 +0000192 bool optimizeExtractElementInst(Instruction *Inst);
193 bool dupRetToEnableTailCallOpts(BasicBlock *BB);
194 bool placeDbgValues(Function &F);
Tim Northovercea0abb2014-03-29 08:22:29 +0000195 bool sinkAndCmp(Function &F);
Sanjay Patelfc580a62015-09-21 23:03:16 +0000196 bool extLdPromotion(TypePromotionTransaction &TPT, LoadInst *&LI,
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000197 Instruction *&Inst,
198 const SmallVectorImpl<Instruction *> &Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +0000199 unsigned CreatedInstCost);
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000200 bool splitBranchCondition(Function &F);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000201 bool simplifyOffsetableRelocate(Instruction &I);
Piotr Padlewski6c15ec42015-09-15 18:32:14 +0000202 void stripInvariantGroupMetadata(Instruction &I);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000203 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000204}
Devang Patel09f162c2007-05-01 21:15:47 +0000205
Devang Patel8c78a0b2007-05-03 01:11:54 +0000206char CodeGenPrepare::ID = 0;
Jiangning Liud623c522014-06-11 07:04:37 +0000207INITIALIZE_TM_PASS(CodeGenPrepare, "codegenprepare",
208 "Optimize for code generation", false, false)
Chris Lattnerf2836d12007-03-31 04:06:36 +0000209
Bill Wendling7a639ea2013-06-19 21:07:11 +0000210FunctionPass *llvm::createCodeGenPreparePass(const TargetMachine *TM) {
211 return new CodeGenPrepare(TM);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000212}
213
Chris Lattnerf2836d12007-03-31 04:06:36 +0000214bool CodeGenPrepare::runOnFunction(Function &F) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000215 if (skipFunction(F))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000216 return false;
217
Mehdi Amini4fe37982015-07-07 18:45:17 +0000218 DL = &F.getParent()->getDataLayout();
219
Chris Lattnerf2836d12007-03-31 04:06:36 +0000220 bool EverMadeChange = false;
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000221 // Clear per function information.
Ahmed Bougachaf3299142015-06-17 20:44:32 +0000222 InsertedInsts.clear();
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000223 PromotedInsts.clear();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000224
Devang Patel8f606d72011-03-24 15:35:25 +0000225 ModifiedDT = false;
Eric Christopherd9134482014-08-04 21:25:23 +0000226 if (TM)
Eric Christopherfccff372015-01-27 01:01:38 +0000227 TLI = TM->getSubtargetImpl(F)->getTargetLowering();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000228 TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthfdb9c572015-02-01 12:01:35 +0000229 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000230 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Sanjay Patel82d91dd2015-08-11 19:39:36 +0000231 OptSize = F.optForSize();
Evan Cheng0663f232011-03-21 01:19:09 +0000232
Preston Gurdcdf540d2012-09-04 18:22:17 +0000233 /// This optimization identifies DIV instructions that can be
234 /// profitably bypassed and carried out with a shorter, faster divide.
Preston Gurd485296d2013-03-04 18:13:57 +0000235 if (!OptSize && TLI && TLI->isSlowDivBypassed()) {
Preston Gurd0d67f512012-10-04 21:33:40 +0000236 const DenseMap<unsigned int, unsigned int> &BypassWidths =
237 TLI->getBypassSlowDivWidths();
Eric Christopher49a7d6c2016-01-04 23:18:58 +0000238 BasicBlock* BB = &*F.begin();
239 while (BB != nullptr) {
240 // bypassSlowDivision may create new BBs, but we don't want to reapply the
241 // optimization to those blocks.
242 BasicBlock* Next = BB->getNextNode();
243 EverMadeChange |= bypassSlowDivision(BB, BypassWidths);
244 BB = Next;
245 }
Preston Gurdcdf540d2012-09-04 18:22:17 +0000246 }
247
248 // Eliminate blocks that contain only PHI nodes and an
Chris Lattnerc3748562007-04-02 01:35:34 +0000249 // unconditional branch.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000250 EverMadeChange |= eliminateMostlyEmptyBlocks(F);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000251
Devang Patel53771ba2011-08-18 00:50:51 +0000252 // llvm.dbg.value is far away from the value then iSel may not be able
Nadav Rotem465834c2012-07-24 10:51:42 +0000253 // handle it properly. iSel will drop llvm.dbg.value if it can not
Devang Patel53771ba2011-08-18 00:50:51 +0000254 // find a node corresponding to the value.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000255 EverMadeChange |= placeDbgValues(F);
Devang Patel53771ba2011-08-18 00:50:51 +0000256
Tim Northovercea0abb2014-03-29 08:22:29 +0000257 // If there is a mask, compare against zero, and branch that can be combined
258 // into a single target instruction, push the mask and compare into branch
259 // users. Do this before OptimizeBlock -> OptimizeInst ->
260 // OptimizeCmpExpression, which perturbs the pattern being searched for.
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000261 if (!DisableBranchOpts) {
Tim Northovercea0abb2014-03-29 08:22:29 +0000262 EverMadeChange |= sinkAndCmp(F);
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000263 EverMadeChange |= splitBranchCondition(F);
264 }
Tim Northovercea0abb2014-03-29 08:22:29 +0000265
Chris Lattnerc3748562007-04-02 01:35:34 +0000266 bool MadeChange = true;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000267 while (MadeChange) {
268 MadeChange = false;
Hans Wennborg02fbc712012-09-19 07:48:16 +0000269 for (Function::iterator I = F.begin(); I != F.end(); ) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000270 BasicBlock *BB = &*I++;
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000271 bool ModifiedDTOnIteration = false;
Sanjay Patelfc580a62015-09-21 23:03:16 +0000272 MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000273
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000274 // Restart BB iteration if the dominator tree of the Function was changed
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000275 if (ModifiedDTOnIteration)
276 break;
Evan Cheng0663f232011-03-21 01:19:09 +0000277 }
Chris Lattnerf2836d12007-03-31 04:06:36 +0000278 EverMadeChange |= MadeChange;
279 }
Cameron Zwarichce3b9302011-01-06 00:42:50 +0000280
281 SunkAddrs.clear();
282
Cameron Zwarich338d3622011-03-11 21:52:04 +0000283 if (!DisableBranchOpts) {
284 MadeChange = false;
Bill Wendling97b93592012-03-04 10:46:01 +0000285 SmallPtrSet<BasicBlock*, 8> WorkList;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +0000286 for (BasicBlock &BB : F) {
287 SmallVector<BasicBlock *, 2> Successors(succ_begin(&BB), succ_end(&BB));
288 MadeChange |= ConstantFoldTerminator(&BB, true);
Bill Wendling97b93592012-03-04 10:46:01 +0000289 if (!MadeChange) continue;
290
291 for (SmallVectorImpl<BasicBlock*>::iterator
292 II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
293 if (pred_begin(*II) == pred_end(*II))
294 WorkList.insert(*II);
295 }
296
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000297 // Delete the dead blocks and any of their dead successors.
Bill Wendlingab417b62012-12-06 00:30:20 +0000298 MadeChange |= !WorkList.empty();
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000299 while (!WorkList.empty()) {
300 BasicBlock *BB = *WorkList.begin();
301 WorkList.erase(BB);
302 SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB));
303
304 DeleteDeadBlock(BB);
Stephen Lin837bba12013-07-15 17:55:02 +0000305
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000306 for (SmallVectorImpl<BasicBlock*>::iterator
307 II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
308 if (pred_begin(*II) == pred_end(*II))
309 WorkList.insert(*II);
310 }
Cameron Zwarich338d3622011-03-11 21:52:04 +0000311
Nadav Rotem70409992012-08-14 05:19:07 +0000312 // Merge pairs of basic blocks with unconditional branches, connected by
313 // a single edge.
314 if (EverMadeChange || MadeChange)
Sanjay Patelfc580a62015-09-21 23:03:16 +0000315 MadeChange |= eliminateFallThrough(F);
Nadav Rotem70409992012-08-14 05:19:07 +0000316
Cameron Zwarich338d3622011-03-11 21:52:04 +0000317 EverMadeChange |= MadeChange;
318 }
319
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000320 if (!DisableGCOpts) {
321 SmallVector<Instruction *, 2> Statepoints;
322 for (BasicBlock &BB : F)
323 for (Instruction &I : BB)
324 if (isStatepoint(I))
325 Statepoints.push_back(&I);
326 for (auto &I : Statepoints)
327 EverMadeChange |= simplifyOffsetableRelocate(*I);
328 }
329
Chris Lattnerf2836d12007-03-31 04:06:36 +0000330 return EverMadeChange;
331}
332
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000333/// Merge basic blocks which are connected by a single edge, where one of the
334/// basic blocks has a single successor pointing to the other basic block,
335/// which has a single predecessor.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000336bool CodeGenPrepare::eliminateFallThrough(Function &F) {
Nadav Rotem70409992012-08-14 05:19:07 +0000337 bool Changed = false;
338 // Scan all of the blocks in the function, except for the entry block.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000339 for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000340 BasicBlock *BB = &*I++;
Nadav Rotem70409992012-08-14 05:19:07 +0000341 // If the destination block has a single pred, then this is a trivial
342 // edge, just collapse it.
343 BasicBlock *SinglePred = BB->getSinglePredecessor();
344
Evan Cheng64a223a2012-09-28 23:58:57 +0000345 // Don't merge if BB's address is taken.
346 if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue;
Nadav Rotem70409992012-08-14 05:19:07 +0000347
348 BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
349 if (Term && !Term->isConditional()) {
350 Changed = true;
Michael Liao6e12d122012-08-21 05:55:22 +0000351 DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n");
Nadav Rotem70409992012-08-14 05:19:07 +0000352 // Remember if SinglePred was the entry block of the function.
353 // If so, we will need to move BB back to the entry position.
354 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
Quentin Colombet7bdd50d2015-03-18 23:17:28 +0000355 MergeBasicBlockIntoOnlyPred(BB, nullptr);
Nadav Rotem70409992012-08-14 05:19:07 +0000356
357 if (isEntry && BB != &BB->getParent()->getEntryBlock())
358 BB->moveBefore(&BB->getParent()->getEntryBlock());
359
360 // We have erased a block. Update the iterator.
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000361 I = BB->getIterator();
Nadav Rotem70409992012-08-14 05:19:07 +0000362 }
363 }
364 return Changed;
365}
366
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000367/// Eliminate blocks that contain only PHI nodes, debug info directives, and an
368/// unconditional branch. Passes before isel (e.g. LSR/loopsimplify) often split
369/// edges in ways that are non-optimal for isel. Start by eliminating these
370/// blocks so we can split them the way we want them.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000371bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) {
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000372 SmallPtrSet<BasicBlock *, 16> Preheaders;
373 SmallVector<Loop *, 16> LoopList(LI->begin(), LI->end());
374 while (!LoopList.empty()) {
375 Loop *L = LoopList.pop_back_val();
376 LoopList.insert(LoopList.end(), L->begin(), L->end());
377 if (BasicBlock *Preheader = L->getLoopPreheader())
378 Preheaders.insert(Preheader);
379 }
380
Chris Lattnerc3748562007-04-02 01:35:34 +0000381 bool MadeChange = false;
382 // Note that this intentionally skips the entry block.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000383 for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000384 BasicBlock *BB = &*I++;
Chris Lattnerc3748562007-04-02 01:35:34 +0000385
386 // If this block doesn't end with an uncond branch, ignore it.
387 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
388 if (!BI || !BI->isUnconditional())
389 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000390
Dale Johannesen4026b042009-03-27 01:13:37 +0000391 // If the instruction before the branch (skipping debug info) isn't a phi
392 // node, then other stuff is happening here.
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000393 BasicBlock::iterator BBI = BI->getIterator();
Chris Lattnerc3748562007-04-02 01:35:34 +0000394 if (BBI != BB->begin()) {
395 --BBI;
Dale Johannesen4026b042009-03-27 01:13:37 +0000396 while (isa<DbgInfoIntrinsic>(BBI)) {
397 if (BBI == BB->begin())
398 break;
399 --BBI;
400 }
401 if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
402 continue;
Chris Lattnerc3748562007-04-02 01:35:34 +0000403 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000404
Chris Lattnerc3748562007-04-02 01:35:34 +0000405 // Do not break infinite loops.
406 BasicBlock *DestBB = BI->getSuccessor(0);
407 if (DestBB == BB)
408 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000409
Sanjay Patelfc580a62015-09-21 23:03:16 +0000410 if (!canMergeBlocks(BB, DestBB))
Chris Lattnerc3748562007-04-02 01:35:34 +0000411 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000412
Chuang-Yu Chengd3fb38c2016-04-05 14:06:20 +0000413 // Do not delete loop preheaders if doing so would create a critical edge.
414 // Loop preheaders can be good locations to spill registers. If the
415 // preheader is deleted and we create a critical edge, registers may be
416 // spilled in the loop body instead.
417 if (!DisablePreheaderProtect && Preheaders.count(BB) &&
418 !(BB->getSinglePredecessor() && BB->getSinglePredecessor()->getSingleSuccessor()))
419 continue;
420
Sanjay Patelfc580a62015-09-21 23:03:16 +0000421 eliminateMostlyEmptyBlock(BB);
Chris Lattnerc3748562007-04-02 01:35:34 +0000422 MadeChange = true;
423 }
424 return MadeChange;
425}
426
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000427/// Return true if we can merge BB into DestBB if there is a single
428/// unconditional branch between them, and BB contains no other non-phi
Chris Lattnerc3748562007-04-02 01:35:34 +0000429/// instructions.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000430bool CodeGenPrepare::canMergeBlocks(const BasicBlock *BB,
Chris Lattnerc3748562007-04-02 01:35:34 +0000431 const BasicBlock *DestBB) const {
432 // We only want to eliminate blocks whose phi nodes are used by phi nodes in
433 // the successor. If there are more complex condition (e.g. preheaders),
434 // don't mess around with them.
435 BasicBlock::const_iterator BBI = BB->begin();
436 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000437 for (const User *U : PN->users()) {
438 const Instruction *UI = cast<Instruction>(U);
439 if (UI->getParent() != DestBB || !isa<PHINode>(UI))
Chris Lattnerc3748562007-04-02 01:35:34 +0000440 return false;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000441 // If User is inside DestBB block and it is a PHINode then check
442 // incoming value. If incoming value is not from BB then this is
Devang Pateld3208522007-04-25 00:37:04 +0000443 // a complex condition (e.g. preheaders) we want to avoid here.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000444 if (UI->getParent() == DestBB) {
445 if (const PHINode *UPN = dyn_cast<PHINode>(UI))
Devang Pateld3208522007-04-25 00:37:04 +0000446 for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
447 Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
448 if (Insn && Insn->getParent() == BB &&
449 Insn->getParent() != UPN->getIncomingBlock(I))
450 return false;
451 }
452 }
Chris Lattnerc3748562007-04-02 01:35:34 +0000453 }
454 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000455
Chris Lattnerc3748562007-04-02 01:35:34 +0000456 // If BB and DestBB contain any common predecessors, then the phi nodes in BB
457 // and DestBB may have conflicting incoming values for the block. If so, we
458 // can't merge the block.
459 const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
460 if (!DestBBPN) return true; // no conflict.
Eric Christopherc1ea1492008-09-24 05:32:41 +0000461
Chris Lattnerc3748562007-04-02 01:35:34 +0000462 // Collect the preds of BB.
Chris Lattner8201a9b2007-11-06 22:07:40 +0000463 SmallPtrSet<const BasicBlock*, 16> BBPreds;
Chris Lattnerc3748562007-04-02 01:35:34 +0000464 if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
465 // It is faster to get preds from a PHI than with pred_iterator.
466 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
467 BBPreds.insert(BBPN->getIncomingBlock(i));
468 } else {
469 BBPreds.insert(pred_begin(BB), pred_end(BB));
470 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000471
Chris Lattnerc3748562007-04-02 01:35:34 +0000472 // Walk the preds of DestBB.
473 for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
474 BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
475 if (BBPreds.count(Pred)) { // Common predecessor?
476 BBI = DestBB->begin();
477 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
478 const Value *V1 = PN->getIncomingValueForBlock(Pred);
479 const Value *V2 = PN->getIncomingValueForBlock(BB);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000480
Chris Lattnerc3748562007-04-02 01:35:34 +0000481 // If V2 is a phi node in BB, look up what the mapped value will be.
482 if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
483 if (V2PN->getParent() == BB)
484 V2 = V2PN->getIncomingValueForBlock(Pred);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000485
Chris Lattnerc3748562007-04-02 01:35:34 +0000486 // If there is a conflict, bail out.
487 if (V1 != V2) return false;
488 }
489 }
490 }
491
492 return true;
493}
494
495
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000496/// Eliminate a basic block that has only phi's and an unconditional branch in
497/// it.
Sanjay Patelfc580a62015-09-21 23:03:16 +0000498void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) {
Chris Lattnerc3748562007-04-02 01:35:34 +0000499 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
500 BasicBlock *DestBB = BI->getSuccessor(0);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000501
David Greene74e2d492010-01-05 01:27:11 +0000502 DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000503
Chris Lattnerc3748562007-04-02 01:35:34 +0000504 // If the destination block has a single pred, then this is a trivial edge,
505 // just collapse it.
Chris Lattner4059f432008-11-27 19:29:14 +0000506 if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
Chris Lattner8a172da2008-11-28 19:54:49 +0000507 if (SinglePred != DestBB) {
508 // Remember if SinglePred was the entry block of the function. If so, we
509 // will need to move BB back to the entry position.
510 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
Quentin Colombet7bdd50d2015-03-18 23:17:28 +0000511 MergeBasicBlockIntoOnlyPred(DestBB, nullptr);
Chris Lattner4059f432008-11-27 19:29:14 +0000512
Chris Lattner8a172da2008-11-28 19:54:49 +0000513 if (isEntry && BB != &BB->getParent()->getEntryBlock())
514 BB->moveBefore(&BB->getParent()->getEntryBlock());
Nadav Rotem465834c2012-07-24 10:51:42 +0000515
David Greene74e2d492010-01-05 01:27:11 +0000516 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattner8a172da2008-11-28 19:54:49 +0000517 return;
518 }
Chris Lattnerc3748562007-04-02 01:35:34 +0000519 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000520
Chris Lattnerc3748562007-04-02 01:35:34 +0000521 // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB
522 // to handle the new incoming edges it is about to have.
523 PHINode *PN;
524 for (BasicBlock::iterator BBI = DestBB->begin();
525 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
526 // Remove the incoming value for BB, and remember it.
527 Value *InVal = PN->removeIncomingValue(BB, false);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000528
Chris Lattnerc3748562007-04-02 01:35:34 +0000529 // Two options: either the InVal is a phi node defined in BB or it is some
530 // value that dominates BB.
531 PHINode *InValPhi = dyn_cast<PHINode>(InVal);
532 if (InValPhi && InValPhi->getParent() == BB) {
533 // Add all of the input values of the input PHI as inputs of this phi.
534 for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
535 PN->addIncoming(InValPhi->getIncomingValue(i),
536 InValPhi->getIncomingBlock(i));
537 } else {
538 // Otherwise, add one instance of the dominating value for each edge that
539 // we will be adding.
540 if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
541 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
542 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
543 } else {
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000544 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
545 PN->addIncoming(InVal, *PI);
Chris Lattnerc3748562007-04-02 01:35:34 +0000546 }
547 }
548 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000549
Chris Lattnerc3748562007-04-02 01:35:34 +0000550 // The PHIs are now updated, change everything that refers to BB to use
551 // DestBB and remove BB.
552 BB->replaceAllUsesWith(DestBB);
553 BB->eraseFromParent();
Cameron Zwarichced753f2011-01-05 17:27:27 +0000554 ++NumBlocksElim;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000555
David Greene74e2d492010-01-05 01:27:11 +0000556 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattnerc3748562007-04-02 01:35:34 +0000557}
558
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000559// Computes a map of base pointer relocation instructions to corresponding
560// derived pointer relocation instructions given a vector of all relocate calls
561static void computeBaseDerivedRelocateMap(
Manuel Jacob83eefa62016-01-05 04:03:00 +0000562 const SmallVectorImpl<GCRelocateInst *> &AllRelocateCalls,
563 DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>>
564 &RelocateInstMap) {
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000565 // Collect information in two maps: one primarily for locating the base object
566 // while filling the second map; the second map is the final structure holding
567 // a mapping between Base and corresponding Derived relocate calls
Manuel Jacob83eefa62016-01-05 04:03:00 +0000568 DenseMap<std::pair<unsigned, unsigned>, GCRelocateInst *> RelocateIdxMap;
569 for (auto *ThisRelocate : AllRelocateCalls) {
570 auto K = std::make_pair(ThisRelocate->getBasePtrIndex(),
571 ThisRelocate->getDerivedPtrIndex());
572 RelocateIdxMap.insert(std::make_pair(K, ThisRelocate));
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000573 }
574 for (auto &Item : RelocateIdxMap) {
575 std::pair<unsigned, unsigned> Key = Item.first;
576 if (Key.first == Key.second)
577 // Base relocation: nothing to insert
578 continue;
579
Manuel Jacob83eefa62016-01-05 04:03:00 +0000580 GCRelocateInst *I = Item.second;
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000581 auto BaseKey = std::make_pair(Key.first, Key.first);
Sanjoy Dasb8186762015-02-27 02:24:16 +0000582
583 // We're iterating over RelocateIdxMap so we cannot modify it.
584 auto MaybeBase = RelocateIdxMap.find(BaseKey);
585 if (MaybeBase == RelocateIdxMap.end())
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000586 // TODO: We might want to insert a new base object relocate and gep off
587 // that, if there are enough derived object relocates.
588 continue;
Sanjoy Dasb8186762015-02-27 02:24:16 +0000589
590 RelocateInstMap[MaybeBase->second].push_back(I);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000591 }
592}
593
594// Accepts a GEP and extracts the operands into a vector provided they're all
595// small integer constants
596static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP,
597 SmallVectorImpl<Value *> &OffsetV) {
598 for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
599 // Only accept small constant integer operands
600 auto Op = dyn_cast<ConstantInt>(GEP->getOperand(i));
601 if (!Op || Op->getZExtValue() > 20)
602 return false;
603 }
604
605 for (unsigned i = 1; i < GEP->getNumOperands(); i++)
606 OffsetV.push_back(GEP->getOperand(i));
607 return true;
608}
609
610// Takes a RelocatedBase (base pointer relocation instruction) and Targets to
611// replace, computes a replacement, and affects it.
612static bool
Manuel Jacob83eefa62016-01-05 04:03:00 +0000613simplifyRelocatesOffABase(GCRelocateInst *RelocatedBase,
614 const SmallVectorImpl<GCRelocateInst *> &Targets) {
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000615 bool MadeChange = false;
Manuel Jacob83eefa62016-01-05 04:03:00 +0000616 for (GCRelocateInst *ToReplace : Targets) {
617 assert(ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() &&
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000618 "Not relocating a derived object of the original base object");
Manuel Jacob83eefa62016-01-05 04:03:00 +0000619 if (ToReplace->getBasePtrIndex() == ToReplace->getDerivedPtrIndex()) {
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000620 // A duplicate relocate call. TODO: coalesce duplicates.
621 continue;
622 }
623
Igor Laevskyf637b4a2015-11-03 18:37:40 +0000624 if (RelocatedBase->getParent() != ToReplace->getParent()) {
625 // Base and derived relocates are in different basic blocks.
626 // In this case transform is only valid when base dominates derived
627 // relocate. However it would be too expensive to check dominance
628 // for each such relocate, so we skip the whole transformation.
629 continue;
630 }
631
Manuel Jacob83eefa62016-01-05 04:03:00 +0000632 Value *Base = ToReplace->getBasePtr();
633 auto Derived = dyn_cast<GetElementPtrInst>(ToReplace->getDerivedPtr());
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000634 if (!Derived || Derived->getPointerOperand() != Base)
635 continue;
636
637 SmallVector<Value *, 2> OffsetV;
638 if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV))
639 continue;
640
641 // Create a Builder and replace the target callsite with a gep
Sanjay Patel545a4562016-01-20 18:59:16 +0000642 assert(RelocatedBase->getNextNode() &&
643 "Should always have one since it's not a terminator");
Sanjoy Das3d705e32015-05-11 23:47:30 +0000644
645 // Insert after RelocatedBase
646 IRBuilder<> Builder(RelocatedBase->getNextNode());
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000647 Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc());
Sanjoy Das89c54912015-05-11 18:49:34 +0000648
649 // If gc_relocate does not match the actual type, cast it to the right type.
650 // In theory, there must be a bitcast after gc_relocate if the type does not
651 // match, and we should reuse it to get the derived pointer. But it could be
652 // cases like this:
653 // bb1:
654 // ...
655 // %g1 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
656 // br label %merge
657 //
658 // bb2:
659 // ...
660 // %g2 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
661 // br label %merge
662 //
663 // merge:
664 // %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ]
665 // %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)*
666 //
667 // In this case, we can not find the bitcast any more. So we insert a new bitcast
668 // no matter there is already one or not. In this way, we can handle all cases, and
669 // the extra bitcast should be optimized away in later passes.
Manuel Jacob5b90b142015-12-19 18:38:42 +0000670 Value *ActualRelocatedBase = RelocatedBase;
Sanjoy Das89c54912015-05-11 18:49:34 +0000671 if (RelocatedBase->getType() != Base->getType()) {
672 ActualRelocatedBase =
Manuel Jacob5b90b142015-12-19 18:38:42 +0000673 Builder.CreateBitCast(RelocatedBase, Base->getType());
Sanjoy Das89c54912015-05-11 18:49:34 +0000674 }
David Blaikie68d535c2015-03-24 22:38:16 +0000675 Value *Replacement = Builder.CreateGEP(
Sanjoy Das89c54912015-05-11 18:49:34 +0000676 Derived->getSourceElementType(), ActualRelocatedBase, makeArrayRef(OffsetV));
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000677 Replacement->takeName(ToReplace);
Sanjoy Das89c54912015-05-11 18:49:34 +0000678 // If the newly generated derived pointer's type does not match the original derived
679 // pointer's type, cast the new derived pointer to match it. Same reasoning as above.
Manuel Jacob5b90b142015-12-19 18:38:42 +0000680 Value *ActualReplacement = Replacement;
681 if (Replacement->getType() != ToReplace->getType()) {
Sanjoy Das89c54912015-05-11 18:49:34 +0000682 ActualReplacement =
Manuel Jacob5b90b142015-12-19 18:38:42 +0000683 Builder.CreateBitCast(Replacement, ToReplace->getType());
Sanjoy Das89c54912015-05-11 18:49:34 +0000684 }
685 ToReplace->replaceAllUsesWith(ActualReplacement);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000686 ToReplace->eraseFromParent();
687
688 MadeChange = true;
689 }
690 return MadeChange;
691}
692
693// Turns this:
694//
695// %base = ...
696// %ptr = gep %base + 15
697// %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
698// %base' = relocate(%tok, i32 4, i32 4)
699// %ptr' = relocate(%tok, i32 4, i32 5)
700// %val = load %ptr'
701//
702// into this:
703//
704// %base = ...
705// %ptr = gep %base + 15
706// %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
707// %base' = gc.relocate(%tok, i32 4, i32 4)
708// %ptr' = gep %base' + 15
709// %val = load %ptr'
710bool CodeGenPrepare::simplifyOffsetableRelocate(Instruction &I) {
711 bool MadeChange = false;
Manuel Jacob83eefa62016-01-05 04:03:00 +0000712 SmallVector<GCRelocateInst *, 2> AllRelocateCalls;
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000713
714 for (auto *U : I.users())
Manuel Jacob83eefa62016-01-05 04:03:00 +0000715 if (GCRelocateInst *Relocate = dyn_cast<GCRelocateInst>(U))
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000716 // Collect all the relocate calls associated with a statepoint
Manuel Jacob83eefa62016-01-05 04:03:00 +0000717 AllRelocateCalls.push_back(Relocate);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000718
719 // We need atleast one base pointer relocation + one derived pointer
720 // relocation to mangle
721 if (AllRelocateCalls.size() < 2)
722 return false;
723
724 // RelocateInstMap is a mapping from the base relocate instruction to the
725 // corresponding derived relocate instructions
Manuel Jacob83eefa62016-01-05 04:03:00 +0000726 DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>> RelocateInstMap;
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000727 computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap);
728 if (RelocateInstMap.empty())
729 return false;
730
731 for (auto &Item : RelocateInstMap)
732 // Item.first is the RelocatedBase to offset against
733 // Item.second is the vector of Targets to replace
734 MadeChange = simplifyRelocatesOffABase(Item.first, Item.second);
735 return MadeChange;
736}
737
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000738/// SinkCast - Sink the specified cast instruction into its user blocks
739static bool SinkCast(CastInst *CI) {
Chris Lattnerf2836d12007-03-31 04:06:36 +0000740 BasicBlock *DefBB = CI->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000741
Chris Lattnerf2836d12007-03-31 04:06:36 +0000742 /// InsertedCasts - Only insert a cast in each block once.
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000743 DenseMap<BasicBlock*, CastInst*> InsertedCasts;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000744
Chris Lattnerf2836d12007-03-31 04:06:36 +0000745 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000746 for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
Chris Lattnerf2836d12007-03-31 04:06:36 +0000747 UI != E; ) {
748 Use &TheUse = UI.getUse();
749 Instruction *User = cast<Instruction>(*UI);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000750
Chris Lattnerf2836d12007-03-31 04:06:36 +0000751 // Figure out which BB this cast is used in. For PHI's this is the
752 // appropriate predecessor block.
753 BasicBlock *UserBB = User->getParent();
754 if (PHINode *PN = dyn_cast<PHINode>(User)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000755 UserBB = PN->getIncomingBlock(TheUse);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000756 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000757
Chris Lattnerf2836d12007-03-31 04:06:36 +0000758 // Preincrement use iterator so we don't invalidate it.
759 ++UI;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000760
David Majnemer0c80e2e2016-04-27 19:36:38 +0000761 // The first insertion point of a block containing an EH pad is after the
762 // pad. If the pad is the user, we cannot sink the cast past the pad.
763 if (User->isEHPad())
764 continue;
765
Andrew Kaylord0430e82015-11-23 19:16:15 +0000766 // If the block selected to receive the cast is an EH pad that does not
767 // allow non-PHI instructions before the terminator, we can't sink the
768 // cast.
769 if (UserBB->getTerminator()->isEHPad())
770 continue;
771
Chris Lattnerf2836d12007-03-31 04:06:36 +0000772 // If this user is in the same block as the cast, don't change the cast.
773 if (UserBB == DefBB) continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000774
Chris Lattnerf2836d12007-03-31 04:06:36 +0000775 // If we have already inserted a cast into this block, use it.
776 CastInst *&InsertedCast = InsertedCasts[UserBB];
777
778 if (!InsertedCast) {
Bill Wendling8ddfc092011-08-16 20:45:24 +0000779 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000780 assert(InsertPt != UserBB->end());
781 InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0),
782 CI->getType(), "", &*InsertPt);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000783 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000784
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000785 // Replace a use of the cast with a use of the new cast.
Chris Lattnerf2836d12007-03-31 04:06:36 +0000786 TheUse = InsertedCast;
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000787 MadeChange = true;
Cameron Zwarichced753f2011-01-05 17:27:27 +0000788 ++NumCastUses;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000789 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000790
Chris Lattnerf2836d12007-03-31 04:06:36 +0000791 // If we removed all uses, nuke the cast.
Duncan Sandsafa84da42008-01-20 16:51:46 +0000792 if (CI->use_empty()) {
Chris Lattnerf2836d12007-03-31 04:06:36 +0000793 CI->eraseFromParent();
Duncan Sandsafa84da42008-01-20 16:51:46 +0000794 MadeChange = true;
795 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000796
Chris Lattnerf2836d12007-03-31 04:06:36 +0000797 return MadeChange;
798}
799
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000800/// If the specified cast instruction is a noop copy (e.g. it's casting from
801/// one pointer type to another, i32->i8 on PPC), sink it into user blocks to
802/// reduce the number of virtual registers that must be created and coalesced.
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000803///
804/// Return true if any changes are made.
805///
Mehdi Amini44ede332015-07-09 02:09:04 +0000806static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI,
807 const DataLayout &DL) {
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000808 // If this is a noop copy,
Mehdi Amini44ede332015-07-09 02:09:04 +0000809 EVT SrcVT = TLI.getValueType(DL, CI->getOperand(0)->getType());
810 EVT DstVT = TLI.getValueType(DL, CI->getType());
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000811
812 // This is an fp<->int conversion?
813 if (SrcVT.isInteger() != DstVT.isInteger())
814 return false;
815
816 // If this is an extension, it will be a zero or sign extension, which
817 // isn't a noop.
818 if (SrcVT.bitsLT(DstVT)) return false;
819
820 // If these values will be promoted, find out what they will be promoted
821 // to. This helps us consider truncates on PPC as noop copies when they
822 // are.
823 if (TLI.getTypeAction(CI->getContext(), SrcVT) ==
824 TargetLowering::TypePromoteInteger)
825 SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
826 if (TLI.getTypeAction(CI->getContext(), DstVT) ==
827 TargetLowering::TypePromoteInteger)
828 DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
829
830 // If, after promotion, these are the same types, this is a noop copy.
831 if (SrcVT != DstVT)
832 return false;
833
834 return SinkCast(CI);
835}
836
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000837/// Try to combine CI into a call to the llvm.uadd.with.overflow intrinsic if
838/// possible.
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000839///
840/// Return true if any changes were made.
841static bool CombineUAddWithOverflow(CmpInst *CI) {
842 Value *A, *B;
843 Instruction *AddI;
844 if (!match(CI,
845 m_UAddWithOverflow(m_Value(A), m_Value(B), m_Instruction(AddI))))
846 return false;
847
848 Type *Ty = AddI->getType();
849 if (!isa<IntegerType>(Ty))
850 return false;
851
852 // We don't want to move around uses of condition values this late, so we we
853 // check if it is legal to create the call to the intrinsic in the basic
854 // block containing the icmp:
855
856 if (AddI->getParent() != CI->getParent() && !AddI->hasOneUse())
857 return false;
858
859#ifndef NDEBUG
860 // Someday m_UAddWithOverflow may get smarter, but this is a safe assumption
861 // for now:
862 if (AddI->hasOneUse())
863 assert(*AddI->user_begin() == CI && "expected!");
864#endif
865
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000866 Module *M = CI->getModule();
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000867 Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty);
868
869 auto *InsertPt = AddI->hasOneUse() ? CI : AddI;
870
871 auto *UAddWithOverflow =
872 CallInst::Create(F, {A, B}, "uadd.overflow", InsertPt);
873 auto *UAdd = ExtractValueInst::Create(UAddWithOverflow, 0, "uadd", InsertPt);
874 auto *Overflow =
875 ExtractValueInst::Create(UAddWithOverflow, 1, "overflow", InsertPt);
876
877 CI->replaceAllUsesWith(Overflow);
878 AddI->replaceAllUsesWith(UAdd);
879 CI->eraseFromParent();
880 AddI->eraseFromParent();
881 return true;
882}
883
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000884/// Sink the given CmpInst into user blocks to reduce the number of virtual
885/// registers that must be created and coalesced. This is a clear win except on
886/// targets with multiple condition code registers (PowerPC), where it might
887/// lose; some adjustment may be wanted there.
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000888///
889/// Return true if any changes are made.
Peter Zotov8efe38a2016-04-03 19:32:13 +0000890static bool SinkCmpExpression(CmpInst *CI, const TargetLowering *TLI) {
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000891 BasicBlock *DefBB = CI->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000892
Peter Zotov0b6d7bc2016-04-03 16:36:17 +0000893 // Avoid sinking soft-FP comparisons, since this can move them into a loop.
Peter Zotov8efe38a2016-04-03 19:32:13 +0000894 if (TLI && TLI->useSoftFloat() && isa<FCmpInst>(CI))
Peter Zotov0b6d7bc2016-04-03 16:36:17 +0000895 return false;
896
897 // Only insert a cmp in each block once.
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000898 DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000899
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000900 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000901 for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000902 UI != E; ) {
903 Use &TheUse = UI.getUse();
904 Instruction *User = cast<Instruction>(*UI);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000905
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000906 // Preincrement use iterator so we don't invalidate it.
907 ++UI;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000908
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000909 // Don't bother for PHI nodes.
910 if (isa<PHINode>(User))
911 continue;
912
913 // Figure out which BB this cmp is used in.
914 BasicBlock *UserBB = User->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000915
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000916 // If this user is in the same block as the cmp, don't change the cmp.
917 if (UserBB == DefBB) continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000918
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000919 // If we have already inserted a cmp into this block, use it.
920 CmpInst *&InsertedCmp = InsertedCmps[UserBB];
921
922 if (!InsertedCmp) {
Bill Wendling8ddfc092011-08-16 20:45:24 +0000923 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000924 assert(InsertPt != UserBB->end());
Eric Christopherc1ea1492008-09-24 05:32:41 +0000925 InsertedCmp =
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000926 CmpInst::Create(CI->getOpcode(), CI->getPredicate(),
927 CI->getOperand(0), CI->getOperand(1), "", &*InsertPt);
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000928 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000929
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000930 // Replace a use of the cmp with a use of the new cmp.
931 TheUse = InsertedCmp;
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000932 MadeChange = true;
Cameron Zwarichced753f2011-01-05 17:27:27 +0000933 ++NumCmpUses;
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000934 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000935
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000936 // If we removed all uses, nuke the cmp.
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000937 if (CI->use_empty()) {
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000938 CI->eraseFromParent();
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000939 MadeChange = true;
940 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000941
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000942 return MadeChange;
943}
944
Peter Zotovf87e5502016-04-03 17:11:53 +0000945static bool OptimizeCmpExpression(CmpInst *CI, const TargetLowering *TLI) {
Peter Zotov8efe38a2016-04-03 19:32:13 +0000946 if (SinkCmpExpression(CI, TLI))
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000947 return true;
948
949 if (CombineUAddWithOverflow(CI))
950 return true;
951
952 return false;
953}
954
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000955/// Check if the candidates could be combined with a shift instruction, which
956/// includes:
Yi Jiangd069f632014-04-21 19:34:27 +0000957/// 1. Truncate instruction
958/// 2. And instruction and the imm is a mask of the low bits:
959/// imm & (imm+1) == 0
Benjamin Kramer322053c2014-04-27 14:54:59 +0000960static bool isExtractBitsCandidateUse(Instruction *User) {
Yi Jiangd069f632014-04-21 19:34:27 +0000961 if (!isa<TruncInst>(User)) {
962 if (User->getOpcode() != Instruction::And ||
963 !isa<ConstantInt>(User->getOperand(1)))
964 return false;
965
Quentin Colombetd4f44692014-04-22 01:20:34 +0000966 const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue();
Yi Jiangd069f632014-04-21 19:34:27 +0000967
Quentin Colombetd4f44692014-04-22 01:20:34 +0000968 if ((Cimm & (Cimm + 1)).getBoolValue())
Yi Jiangd069f632014-04-21 19:34:27 +0000969 return false;
970 }
971 return true;
972}
973
Sanjay Patel4ac6b112015-09-21 22:47:23 +0000974/// Sink both shift and truncate instruction to the use of truncate's BB.
Benjamin Kramer322053c2014-04-27 14:54:59 +0000975static bool
Yi Jiangd069f632014-04-21 19:34:27 +0000976SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
977 DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts,
Mehdi Amini44ede332015-07-09 02:09:04 +0000978 const TargetLowering &TLI, const DataLayout &DL) {
Yi Jiangd069f632014-04-21 19:34:27 +0000979 BasicBlock *UserBB = User->getParent();
980 DenseMap<BasicBlock *, CastInst *> InsertedTruncs;
981 TruncInst *TruncI = dyn_cast<TruncInst>(User);
982 bool MadeChange = false;
983
984 for (Value::user_iterator TruncUI = TruncI->user_begin(),
985 TruncE = TruncI->user_end();
986 TruncUI != TruncE;) {
987
988 Use &TruncTheUse = TruncUI.getUse();
989 Instruction *TruncUser = cast<Instruction>(*TruncUI);
990 // Preincrement use iterator so we don't invalidate it.
991
992 ++TruncUI;
993
994 int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode());
995 if (!ISDOpcode)
996 continue;
997
Tim Northovere2239ff2014-07-29 10:20:22 +0000998 // If the use is actually a legal node, there will not be an
999 // implicit truncate.
1000 // FIXME: always querying the result type is just an
1001 // approximation; some nodes' legality is determined by the
1002 // operand or other means. There's no good way to find out though.
Ahmed Bougacha0788d492014-11-12 22:16:55 +00001003 if (TLI.isOperationLegalOrCustom(
Mehdi Amini44ede332015-07-09 02:09:04 +00001004 ISDOpcode, TLI.getValueType(DL, TruncUser->getType(), true)))
Yi Jiangd069f632014-04-21 19:34:27 +00001005 continue;
1006
1007 // Don't bother for PHI nodes.
1008 if (isa<PHINode>(TruncUser))
1009 continue;
1010
1011 BasicBlock *TruncUserBB = TruncUser->getParent();
1012
1013 if (UserBB == TruncUserBB)
1014 continue;
1015
1016 BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB];
1017 CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB];
1018
1019 if (!InsertedShift && !InsertedTrunc) {
1020 BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001021 assert(InsertPt != TruncUserBB->end());
Yi Jiangd069f632014-04-21 19:34:27 +00001022 // Sink the shift
1023 if (ShiftI->getOpcode() == Instruction::AShr)
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001024 InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI,
1025 "", &*InsertPt);
Yi Jiangd069f632014-04-21 19:34:27 +00001026 else
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001027 InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI,
1028 "", &*InsertPt);
Yi Jiangd069f632014-04-21 19:34:27 +00001029
1030 // Sink the trunc
1031 BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt();
1032 TruncInsertPt++;
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001033 assert(TruncInsertPt != TruncUserBB->end());
Yi Jiangd069f632014-04-21 19:34:27 +00001034
1035 InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift,
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001036 TruncI->getType(), "", &*TruncInsertPt);
Yi Jiangd069f632014-04-21 19:34:27 +00001037
1038 MadeChange = true;
1039
1040 TruncTheUse = InsertedTrunc;
1041 }
1042 }
1043 return MadeChange;
1044}
1045
Sanjay Patel4ac6b112015-09-21 22:47:23 +00001046/// Sink the shift *right* instruction into user blocks if the uses could
1047/// potentially be combined with this shift instruction and generate BitExtract
1048/// instruction. It will only be applied if the architecture supports BitExtract
1049/// instruction. Here is an example:
Yi Jiangd069f632014-04-21 19:34:27 +00001050/// BB1:
1051/// %x.extract.shift = lshr i64 %arg1, 32
1052/// BB2:
1053/// %x.extract.trunc = trunc i64 %x.extract.shift to i16
1054/// ==>
1055///
1056/// BB2:
1057/// %x.extract.shift.1 = lshr i64 %arg1, 32
1058/// %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16
1059///
1060/// CodeGen will recoginze the pattern in BB2 and generate BitExtract
1061/// instruction.
1062/// Return true if any changes are made.
1063static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI,
Mehdi Amini44ede332015-07-09 02:09:04 +00001064 const TargetLowering &TLI,
1065 const DataLayout &DL) {
Yi Jiangd069f632014-04-21 19:34:27 +00001066 BasicBlock *DefBB = ShiftI->getParent();
1067
1068 /// Only insert instructions in each block once.
1069 DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts;
1070
Mehdi Amini44ede332015-07-09 02:09:04 +00001071 bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(DL, ShiftI->getType()));
Yi Jiangd069f632014-04-21 19:34:27 +00001072
1073 bool MadeChange = false;
1074 for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end();
1075 UI != E;) {
1076 Use &TheUse = UI.getUse();
1077 Instruction *User = cast<Instruction>(*UI);
1078 // Preincrement use iterator so we don't invalidate it.
1079 ++UI;
1080
1081 // Don't bother for PHI nodes.
1082 if (isa<PHINode>(User))
1083 continue;
1084
1085 if (!isExtractBitsCandidateUse(User))
1086 continue;
1087
1088 BasicBlock *UserBB = User->getParent();
1089
1090 if (UserBB == DefBB) {
1091 // If the shift and truncate instruction are in the same BB. The use of
1092 // the truncate(TruncUse) may still introduce another truncate if not
1093 // legal. In this case, we would like to sink both shift and truncate
1094 // instruction to the BB of TruncUse.
1095 // for example:
1096 // BB1:
1097 // i64 shift.result = lshr i64 opnd, imm
1098 // trunc.result = trunc shift.result to i16
1099 //
1100 // BB2:
1101 // ----> We will have an implicit truncate here if the architecture does
1102 // not have i16 compare.
1103 // cmp i16 trunc.result, opnd2
1104 //
1105 if (isa<TruncInst>(User) && shiftIsLegal
1106 // If the type of the truncate is legal, no trucate will be
1107 // introduced in other basic blocks.
Mehdi Amini44ede332015-07-09 02:09:04 +00001108 &&
1109 (!TLI.isTypeLegal(TLI.getValueType(DL, User->getType()))))
Yi Jiangd069f632014-04-21 19:34:27 +00001110 MadeChange =
Mehdi Amini44ede332015-07-09 02:09:04 +00001111 SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI, DL);
Yi Jiangd069f632014-04-21 19:34:27 +00001112
1113 continue;
1114 }
1115 // If we have already inserted a shift into this block, use it.
1116 BinaryOperator *&InsertedShift = InsertedShifts[UserBB];
1117
1118 if (!InsertedShift) {
1119 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001120 assert(InsertPt != UserBB->end());
Yi Jiangd069f632014-04-21 19:34:27 +00001121
1122 if (ShiftI->getOpcode() == Instruction::AShr)
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001123 InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI,
1124 "", &*InsertPt);
Yi Jiangd069f632014-04-21 19:34:27 +00001125 else
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001126 InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI,
1127 "", &*InsertPt);
Yi Jiangd069f632014-04-21 19:34:27 +00001128
1129 MadeChange = true;
1130 }
1131
1132 // Replace a use of the shift with a use of the new shift.
1133 TheUse = InsertedShift;
1134 }
1135
1136 // If we removed all uses, nuke the shift.
1137 if (ShiftI->use_empty())
1138 ShiftI->eraseFromParent();
1139
1140 return MadeChange;
1141}
1142
Sanjay Patel4ac6b112015-09-21 22:47:23 +00001143// Translate a masked load intrinsic like
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001144// <16 x i32 > @llvm.masked.load( <16 x i32>* %addr, i32 align,
1145// <16 x i1> %mask, <16 x i32> %passthru)
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001146// to a chain of basic blocks, with loading element one-by-one if
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001147// the appropriate mask bit is set
Junmo Parkaa9243a2016-01-08 04:20:32 +00001148//
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001149// %1 = bitcast i8* %addr to i32*
1150// %2 = extractelement <16 x i1> %mask, i32 0
1151// %3 = icmp eq i1 %2, true
1152// br i1 %3, label %cond.load, label %else
1153//
1154//cond.load: ; preds = %0
1155// %4 = getelementptr i32* %1, i32 0
1156// %5 = load i32* %4
1157// %6 = insertelement <16 x i32> undef, i32 %5, i32 0
1158// br label %else
1159//
1160//else: ; preds = %0, %cond.load
1161// %res.phi.else = phi <16 x i32> [ %6, %cond.load ], [ undef, %0 ]
1162// %7 = extractelement <16 x i1> %mask, i32 1
1163// %8 = icmp eq i1 %7, true
1164// br i1 %8, label %cond.load1, label %else2
1165//
1166//cond.load1: ; preds = %else
1167// %9 = getelementptr i32* %1, i32 1
1168// %10 = load i32* %9
1169// %11 = insertelement <16 x i32> %res.phi.else, i32 %10, i32 1
1170// br label %else2
1171//
1172//else2: ; preds = %else, %cond.load1
1173// %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ]
1174// %12 = extractelement <16 x i1> %mask, i32 2
1175// %13 = icmp eq i1 %12, true
1176// br i1 %13, label %cond.load4, label %else5
1177//
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001178static void scalarizeMaskedLoad(CallInst *CI) {
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001179 Value *Ptr = CI->getArgOperand(0);
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001180 Value *Alignment = CI->getArgOperand(1);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001181 Value *Mask = CI->getArgOperand(2);
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001182 Value *Src0 = CI->getArgOperand(3);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001183
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001184 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
1185 VectorType *VecType = dyn_cast<VectorType>(CI->getType());
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001186 assert(VecType && "Unexpected return type of masked load intrinsic");
1187
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001188 Type *EltTy = CI->getType()->getVectorElementType();
1189
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001190 IRBuilder<> Builder(CI->getContext());
1191 Instruction *InsertPt = CI;
1192 BasicBlock *IfBlock = CI->getParent();
1193 BasicBlock *CondBlock = nullptr;
1194 BasicBlock *PrevIfBlock = CI->getParent();
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001195
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001196 Builder.SetInsertPoint(InsertPt);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001197 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1198
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001199 // Short-cut if the mask is all-true.
1200 bool IsAllOnesMask = isa<Constant>(Mask) &&
1201 cast<Constant>(Mask)->isAllOnesValue();
1202
1203 if (IsAllOnesMask) {
1204 Value *NewI = Builder.CreateAlignedLoad(Ptr, AlignVal);
1205 CI->replaceAllUsesWith(NewI);
1206 CI->eraseFromParent();
1207 return;
1208 }
1209
1210 // Adjust alignment for the scalar instruction.
1211 AlignVal = std::min(AlignVal, VecType->getScalarSizeInBits()/8);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001212 // Bitcast %addr fron i8* to EltTy*
1213 Type *NewPtrType =
1214 EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace());
1215 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001216 unsigned VectorWidth = VecType->getNumElements();
1217
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001218 Value *UndefVal = UndefValue::get(VecType);
1219
1220 // The result vector
1221 Value *VResult = UndefVal;
1222
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001223 if (isa<ConstantVector>(Mask)) {
1224 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1225 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
1226 continue;
1227 Value *Gep =
1228 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
1229 LoadInst* Load = Builder.CreateAlignedLoad(Gep, AlignVal);
1230 VResult = Builder.CreateInsertElement(VResult, Load,
1231 Builder.getInt32(Idx));
1232 }
1233 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
1234 CI->replaceAllUsesWith(NewI);
1235 CI->eraseFromParent();
1236 return;
1237 }
1238
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001239 PHINode *Phi = nullptr;
1240 Value *PrevPhi = UndefVal;
1241
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001242 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1243
1244 // Fill the "else" block, created in the previous iteration
1245 //
1246 // %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ]
1247 // %mask_1 = extractelement <16 x i1> %mask, i32 Idx
1248 // %to_load = icmp eq i1 %mask_1, true
1249 // br i1 %to_load, label %cond.load, label %else
1250 //
1251 if (Idx > 0) {
1252 Phi = Builder.CreatePHI(VecType, 2, "res.phi.else");
1253 Phi->addIncoming(VResult, CondBlock);
1254 Phi->addIncoming(PrevPhi, PrevIfBlock);
1255 PrevPhi = Phi;
1256 VResult = Phi;
1257 }
1258
1259 Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
1260 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1261 ConstantInt::get(Predicate->getType(), 1));
1262
1263 // Create "cond" block
1264 //
1265 // %EltAddr = getelementptr i32* %1, i32 0
1266 // %Elt = load i32* %EltAddr
1267 // VResult = insertelement <16 x i32> VResult, i32 %Elt, i32 Idx
1268 //
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001269 CondBlock = IfBlock->splitBasicBlock(InsertPt->getIterator(), "cond.load");
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001270 Builder.SetInsertPoint(InsertPt);
David Blaikieaa41cd52015-04-03 21:33:42 +00001271
1272 Value *Gep =
1273 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
Elena Demikhovsky09285852015-10-25 15:37:55 +00001274 LoadInst *Load = Builder.CreateAlignedLoad(Gep, AlignVal);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001275 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
1276
1277 // Create "else" block, fill it in the next iteration
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001278 BasicBlock *NewIfBlock =
1279 CondBlock->splitBasicBlock(InsertPt->getIterator(), "else");
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001280 Builder.SetInsertPoint(InsertPt);
1281 Instruction *OldBr = IfBlock->getTerminator();
1282 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1283 OldBr->eraseFromParent();
1284 PrevIfBlock = IfBlock;
1285 IfBlock = NewIfBlock;
1286 }
1287
1288 Phi = Builder.CreatePHI(VecType, 2, "res.phi.select");
1289 Phi->addIncoming(VResult, CondBlock);
1290 Phi->addIncoming(PrevPhi, PrevIfBlock);
1291 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
1292 CI->replaceAllUsesWith(NewI);
1293 CI->eraseFromParent();
1294}
1295
Sanjay Patel4ac6b112015-09-21 22:47:23 +00001296// Translate a masked store intrinsic, like
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001297// void @llvm.masked.store(<16 x i32> %src, <16 x i32>* %addr, i32 align,
1298// <16 x i1> %mask)
1299// to a chain of basic blocks, that stores element one-by-one if
1300// the appropriate mask bit is set
1301//
1302// %1 = bitcast i8* %addr to i32*
1303// %2 = extractelement <16 x i1> %mask, i32 0
1304// %3 = icmp eq i1 %2, true
1305// br i1 %3, label %cond.store, label %else
1306//
1307// cond.store: ; preds = %0
1308// %4 = extractelement <16 x i32> %val, i32 0
1309// %5 = getelementptr i32* %1, i32 0
1310// store i32 %4, i32* %5
1311// br label %else
Junmo Parkaa9243a2016-01-08 04:20:32 +00001312//
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001313// else: ; preds = %0, %cond.store
1314// %6 = extractelement <16 x i1> %mask, i32 1
1315// %7 = icmp eq i1 %6, true
1316// br i1 %7, label %cond.store1, label %else2
Junmo Parkaa9243a2016-01-08 04:20:32 +00001317//
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001318// cond.store1: ; preds = %else
1319// %8 = extractelement <16 x i32> %val, i32 1
1320// %9 = getelementptr i32* %1, i32 1
1321// store i32 %8, i32* %9
1322// br label %else2
1323// . . .
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001324static void scalarizeMaskedStore(CallInst *CI) {
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001325 Value *Src = CI->getArgOperand(0);
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001326 Value *Ptr = CI->getArgOperand(1);
1327 Value *Alignment = CI->getArgOperand(2);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001328 Value *Mask = CI->getArgOperand(3);
1329
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001330 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001331 VectorType *VecType = dyn_cast<VectorType>(Src->getType());
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001332 assert(VecType && "Unexpected data type in masked store intrinsic");
1333
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001334 Type *EltTy = VecType->getElementType();
1335
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001336 IRBuilder<> Builder(CI->getContext());
1337 Instruction *InsertPt = CI;
1338 BasicBlock *IfBlock = CI->getParent();
1339 Builder.SetInsertPoint(InsertPt);
1340 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1341
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001342 // Short-cut if the mask is all-true.
1343 bool IsAllOnesMask = isa<Constant>(Mask) &&
1344 cast<Constant>(Mask)->isAllOnesValue();
1345
1346 if (IsAllOnesMask) {
1347 Builder.CreateAlignedStore(Src, Ptr, AlignVal);
1348 CI->eraseFromParent();
1349 return;
1350 }
1351
1352 // Adjust alignment for the scalar instruction.
1353 AlignVal = std::max(AlignVal, VecType->getScalarSizeInBits()/8);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001354 // Bitcast %addr fron i8* to EltTy*
1355 Type *NewPtrType =
1356 EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace());
1357 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001358 unsigned VectorWidth = VecType->getNumElements();
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001359
1360 if (isa<ConstantVector>(Mask)) {
1361 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1362 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
1363 continue;
1364 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
1365 Value *Gep =
1366 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
1367 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
1368 }
1369 CI->eraseFromParent();
1370 return;
1371 }
1372
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001373 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1374
1375 // Fill the "else" block, created in the previous iteration
1376 //
1377 // %mask_1 = extractelement <16 x i1> %mask, i32 Idx
1378 // %to_store = icmp eq i1 %mask_1, true
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001379 // br i1 %to_store, label %cond.store, label %else
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001380 //
1381 Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
1382 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1383 ConstantInt::get(Predicate->getType(), 1));
1384
1385 // Create "cond" block
1386 //
1387 // %OneElt = extractelement <16 x i32> %Src, i32 Idx
1388 // %EltAddr = getelementptr i32* %1, i32 0
1389 // %store i32 %OneElt, i32* %EltAddr
1390 //
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001391 BasicBlock *CondBlock =
1392 IfBlock->splitBasicBlock(InsertPt->getIterator(), "cond.store");
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001393 Builder.SetInsertPoint(InsertPt);
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001394
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001395 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
David Blaikieaa41cd52015-04-03 21:33:42 +00001396 Value *Gep =
1397 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
Elena Demikhovsky3ad76a12015-10-21 11:50:54 +00001398 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001399
1400 // Create "else" block, fill it in the next iteration
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00001401 BasicBlock *NewIfBlock =
1402 CondBlock->splitBasicBlock(InsertPt->getIterator(), "else");
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001403 Builder.SetInsertPoint(InsertPt);
1404 Instruction *OldBr = IfBlock->getTerminator();
1405 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1406 OldBr->eraseFromParent();
1407 IfBlock = NewIfBlock;
1408 }
1409 CI->eraseFromParent();
1410}
1411
Elena Demikhovsky09285852015-10-25 15:37:55 +00001412// Translate a masked gather intrinsic like
1413// <16 x i32 > @llvm.masked.gather.v16i32( <16 x i32*> %Ptrs, i32 4,
1414// <16 x i1> %Mask, <16 x i32> %Src)
1415// to a chain of basic blocks, with loading element one-by-one if
1416// the appropriate mask bit is set
Junmo Parkaa9243a2016-01-08 04:20:32 +00001417//
Elena Demikhovsky09285852015-10-25 15:37:55 +00001418// % Ptrs = getelementptr i32, i32* %base, <16 x i64> %ind
1419// % Mask0 = extractelement <16 x i1> %Mask, i32 0
1420// % ToLoad0 = icmp eq i1 % Mask0, true
1421// br i1 % ToLoad0, label %cond.load, label %else
Junmo Parkaa9243a2016-01-08 04:20:32 +00001422//
Elena Demikhovsky09285852015-10-25 15:37:55 +00001423// cond.load:
1424// % Ptr0 = extractelement <16 x i32*> %Ptrs, i32 0
1425// % Load0 = load i32, i32* % Ptr0, align 4
1426// % Res0 = insertelement <16 x i32> undef, i32 % Load0, i32 0
1427// br label %else
Junmo Parkaa9243a2016-01-08 04:20:32 +00001428//
Elena Demikhovsky09285852015-10-25 15:37:55 +00001429// else:
1430// %res.phi.else = phi <16 x i32>[% Res0, %cond.load], [undef, % 0]
1431// % Mask1 = extractelement <16 x i1> %Mask, i32 1
1432// % ToLoad1 = icmp eq i1 % Mask1, true
1433// br i1 % ToLoad1, label %cond.load1, label %else2
Junmo Parkaa9243a2016-01-08 04:20:32 +00001434//
Elena Demikhovsky09285852015-10-25 15:37:55 +00001435// cond.load1:
1436// % Ptr1 = extractelement <16 x i32*> %Ptrs, i32 1
1437// % Load1 = load i32, i32* % Ptr1, align 4
1438// % Res1 = insertelement <16 x i32> %res.phi.else, i32 % Load1, i32 1
1439// br label %else2
1440// . . .
1441// % Result = select <16 x i1> %Mask, <16 x i32> %res.phi.select, <16 x i32> %Src
1442// ret <16 x i32> %Result
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001443static void scalarizeMaskedGather(CallInst *CI) {
Elena Demikhovsky09285852015-10-25 15:37:55 +00001444 Value *Ptrs = CI->getArgOperand(0);
1445 Value *Alignment = CI->getArgOperand(1);
1446 Value *Mask = CI->getArgOperand(2);
1447 Value *Src0 = CI->getArgOperand(3);
1448
1449 VectorType *VecType = dyn_cast<VectorType>(CI->getType());
1450
1451 assert(VecType && "Unexpected return type of masked load intrinsic");
1452
1453 IRBuilder<> Builder(CI->getContext());
1454 Instruction *InsertPt = CI;
1455 BasicBlock *IfBlock = CI->getParent();
1456 BasicBlock *CondBlock = nullptr;
1457 BasicBlock *PrevIfBlock = CI->getParent();
1458 Builder.SetInsertPoint(InsertPt);
1459 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
1460
1461 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1462
1463 Value *UndefVal = UndefValue::get(VecType);
1464
1465 // The result vector
1466 Value *VResult = UndefVal;
1467 unsigned VectorWidth = VecType->getNumElements();
1468
1469 // Shorten the way if the mask is a vector of constants.
1470 bool IsConstMask = isa<ConstantVector>(Mask);
1471
1472 if (IsConstMask) {
1473 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1474 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
1475 continue;
1476 Value *Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
1477 "Ptr" + Twine(Idx));
1478 LoadInst *Load = Builder.CreateAlignedLoad(Ptr, AlignVal,
1479 "Load" + Twine(Idx));
1480 VResult = Builder.CreateInsertElement(VResult, Load,
1481 Builder.getInt32(Idx),
1482 "Res" + Twine(Idx));
1483 }
1484 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
1485 CI->replaceAllUsesWith(NewI);
1486 CI->eraseFromParent();
1487 return;
1488 }
1489
1490 PHINode *Phi = nullptr;
1491 Value *PrevPhi = UndefVal;
1492
1493 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1494
1495 // Fill the "else" block, created in the previous iteration
1496 //
1497 // %Mask1 = extractelement <16 x i1> %Mask, i32 1
1498 // %ToLoad1 = icmp eq i1 %Mask1, true
1499 // br i1 %ToLoad1, label %cond.load, label %else
1500 //
1501 if (Idx > 0) {
1502 Phi = Builder.CreatePHI(VecType, 2, "res.phi.else");
1503 Phi->addIncoming(VResult, CondBlock);
1504 Phi->addIncoming(PrevPhi, PrevIfBlock);
1505 PrevPhi = Phi;
1506 VResult = Phi;
1507 }
1508
1509 Value *Predicate = Builder.CreateExtractElement(Mask,
1510 Builder.getInt32(Idx),
1511 "Mask" + Twine(Idx));
1512 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1513 ConstantInt::get(Predicate->getType(), 1),
1514 "ToLoad" + Twine(Idx));
1515
1516 // Create "cond" block
1517 //
1518 // %EltAddr = getelementptr i32* %1, i32 0
1519 // %Elt = load i32* %EltAddr
1520 // VResult = insertelement <16 x i32> VResult, i32 %Elt, i32 Idx
1521 //
1522 CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.load");
1523 Builder.SetInsertPoint(InsertPt);
1524
1525 Value *Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
1526 "Ptr" + Twine(Idx));
1527 LoadInst *Load = Builder.CreateAlignedLoad(Ptr, AlignVal,
1528 "Load" + Twine(Idx));
1529 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx),
1530 "Res" + Twine(Idx));
1531
1532 // Create "else" block, fill it in the next iteration
1533 BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else");
1534 Builder.SetInsertPoint(InsertPt);
1535 Instruction *OldBr = IfBlock->getTerminator();
1536 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1537 OldBr->eraseFromParent();
1538 PrevIfBlock = IfBlock;
1539 IfBlock = NewIfBlock;
1540 }
1541
1542 Phi = Builder.CreatePHI(VecType, 2, "res.phi.select");
1543 Phi->addIncoming(VResult, CondBlock);
1544 Phi->addIncoming(PrevPhi, PrevIfBlock);
1545 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
1546 CI->replaceAllUsesWith(NewI);
1547 CI->eraseFromParent();
1548}
1549
1550// Translate a masked scatter intrinsic, like
1551// void @llvm.masked.scatter.v16i32(<16 x i32> %Src, <16 x i32*>* %Ptrs, i32 4,
1552// <16 x i1> %Mask)
1553// to a chain of basic blocks, that stores element one-by-one if
1554// the appropriate mask bit is set.
1555//
1556// % Ptrs = getelementptr i32, i32* %ptr, <16 x i64> %ind
1557// % Mask0 = extractelement <16 x i1> % Mask, i32 0
1558// % ToStore0 = icmp eq i1 % Mask0, true
1559// br i1 %ToStore0, label %cond.store, label %else
1560//
1561// cond.store:
1562// % Elt0 = extractelement <16 x i32> %Src, i32 0
1563// % Ptr0 = extractelement <16 x i32*> %Ptrs, i32 0
1564// store i32 %Elt0, i32* % Ptr0, align 4
1565// br label %else
Junmo Parkaa9243a2016-01-08 04:20:32 +00001566//
Elena Demikhovsky09285852015-10-25 15:37:55 +00001567// else:
1568// % Mask1 = extractelement <16 x i1> % Mask, i32 1
1569// % ToStore1 = icmp eq i1 % Mask1, true
1570// br i1 % ToStore1, label %cond.store1, label %else2
1571//
1572// cond.store1:
1573// % Elt1 = extractelement <16 x i32> %Src, i32 1
1574// % Ptr1 = extractelement <16 x i32*> %Ptrs, i32 1
1575// store i32 % Elt1, i32* % Ptr1, align 4
1576// br label %else2
1577// . . .
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001578static void scalarizeMaskedScatter(CallInst *CI) {
Elena Demikhovsky09285852015-10-25 15:37:55 +00001579 Value *Src = CI->getArgOperand(0);
1580 Value *Ptrs = CI->getArgOperand(1);
1581 Value *Alignment = CI->getArgOperand(2);
1582 Value *Mask = CI->getArgOperand(3);
1583
1584 assert(isa<VectorType>(Src->getType()) &&
1585 "Unexpected data type in masked scatter intrinsic");
1586 assert(isa<VectorType>(Ptrs->getType()) &&
1587 isa<PointerType>(Ptrs->getType()->getVectorElementType()) &&
1588 "Vector of pointers is expected in masked scatter intrinsic");
1589
1590 IRBuilder<> Builder(CI->getContext());
1591 Instruction *InsertPt = CI;
1592 BasicBlock *IfBlock = CI->getParent();
1593 Builder.SetInsertPoint(InsertPt);
1594 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1595
1596 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
1597 unsigned VectorWidth = Src->getType()->getVectorNumElements();
1598
1599 // Shorten the way if the mask is a vector of constants.
1600 bool IsConstMask = isa<ConstantVector>(Mask);
1601
1602 if (IsConstMask) {
1603 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1604 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
1605 continue;
1606 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
1607 "Elt" + Twine(Idx));
1608 Value *Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
1609 "Ptr" + Twine(Idx));
1610 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
1611 }
1612 CI->eraseFromParent();
1613 return;
1614 }
1615 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1616 // Fill the "else" block, created in the previous iteration
1617 //
1618 // % Mask1 = extractelement <16 x i1> % Mask, i32 Idx
1619 // % ToStore = icmp eq i1 % Mask1, true
1620 // br i1 % ToStore, label %cond.store, label %else
1621 //
1622 Value *Predicate = Builder.CreateExtractElement(Mask,
1623 Builder.getInt32(Idx),
1624 "Mask" + Twine(Idx));
1625 Value *Cmp =
1626 Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1627 ConstantInt::get(Predicate->getType(), 1),
1628 "ToStore" + Twine(Idx));
1629
1630 // Create "cond" block
1631 //
1632 // % Elt1 = extractelement <16 x i32> %Src, i32 1
1633 // % Ptr1 = extractelement <16 x i32*> %Ptrs, i32 1
1634 // %store i32 % Elt1, i32* % Ptr1
1635 //
1636 BasicBlock *CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.store");
1637 Builder.SetInsertPoint(InsertPt);
1638
1639 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
1640 "Elt" + Twine(Idx));
1641 Value *Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
1642 "Ptr" + Twine(Idx));
1643 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
1644
1645 // Create "else" block, fill it in the next iteration
1646 BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else");
1647 Builder.SetInsertPoint(InsertPt);
1648 Instruction *OldBr = IfBlock->getTerminator();
1649 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1650 OldBr->eraseFromParent();
1651 IfBlock = NewIfBlock;
1652 }
1653 CI->eraseFromParent();
1654}
1655
Sanjay Patel4699b8a2015-11-19 16:37:10 +00001656/// If counting leading or trailing zeros is an expensive operation and a zero
1657/// input is defined, add a check for zero to avoid calling the intrinsic.
1658///
1659/// We want to transform:
1660/// %z = call i64 @llvm.cttz.i64(i64 %A, i1 false)
1661///
1662/// into:
1663/// entry:
1664/// %cmpz = icmp eq i64 %A, 0
1665/// br i1 %cmpz, label %cond.end, label %cond.false
1666/// cond.false:
1667/// %z = call i64 @llvm.cttz.i64(i64 %A, i1 true)
1668/// br label %cond.end
1669/// cond.end:
1670/// %ctz = phi i64 [ 64, %entry ], [ %z, %cond.false ]
1671///
1672/// If the transform is performed, return true and set ModifiedDT to true.
1673static bool despeculateCountZeros(IntrinsicInst *CountZeros,
1674 const TargetLowering *TLI,
1675 const DataLayout *DL,
1676 bool &ModifiedDT) {
1677 if (!TLI || !DL)
1678 return false;
1679
1680 // If a zero input is undefined, it doesn't make sense to despeculate that.
1681 if (match(CountZeros->getOperand(1), m_One()))
1682 return false;
1683
1684 // If it's cheap to speculate, there's nothing to do.
1685 auto IntrinsicID = CountZeros->getIntrinsicID();
1686 if ((IntrinsicID == Intrinsic::cttz && TLI->isCheapToSpeculateCttz()) ||
1687 (IntrinsicID == Intrinsic::ctlz && TLI->isCheapToSpeculateCtlz()))
1688 return false;
1689
1690 // Only handle legal scalar cases. Anything else requires too much work.
1691 Type *Ty = CountZeros->getType();
1692 unsigned SizeInBits = Ty->getPrimitiveSizeInBits();
Jun Bum Limbe11bdc2016-05-13 18:38:35 +00001693 if (Ty->isVectorTy() || SizeInBits > DL->getLargestLegalIntTypeSizeInBits())
Sanjay Patel4699b8a2015-11-19 16:37:10 +00001694 return false;
1695
1696 // The intrinsic will be sunk behind a compare against zero and branch.
1697 BasicBlock *StartBlock = CountZeros->getParent();
1698 BasicBlock *CallBlock = StartBlock->splitBasicBlock(CountZeros, "cond.false");
1699
1700 // Create another block after the count zero intrinsic. A PHI will be added
1701 // in this block to select the result of the intrinsic or the bit-width
1702 // constant if the input to the intrinsic is zero.
1703 BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(CountZeros));
1704 BasicBlock *EndBlock = CallBlock->splitBasicBlock(SplitPt, "cond.end");
1705
1706 // Set up a builder to create a compare, conditional branch, and PHI.
1707 IRBuilder<> Builder(CountZeros->getContext());
1708 Builder.SetInsertPoint(StartBlock->getTerminator());
1709 Builder.SetCurrentDebugLocation(CountZeros->getDebugLoc());
1710
1711 // Replace the unconditional branch that was created by the first split with
1712 // a compare against zero and a conditional branch.
1713 Value *Zero = Constant::getNullValue(Ty);
1714 Value *Cmp = Builder.CreateICmpEQ(CountZeros->getOperand(0), Zero, "cmpz");
1715 Builder.CreateCondBr(Cmp, EndBlock, CallBlock);
1716 StartBlock->getTerminator()->eraseFromParent();
1717
1718 // Create a PHI in the end block to select either the output of the intrinsic
1719 // or the bit width of the operand.
1720 Builder.SetInsertPoint(&EndBlock->front());
1721 PHINode *PN = Builder.CreatePHI(Ty, 2, "ctz");
1722 CountZeros->replaceAllUsesWith(PN);
1723 Value *BitWidth = Builder.getInt(APInt(SizeInBits, SizeInBits));
1724 PN->addIncoming(BitWidth, StartBlock);
1725 PN->addIncoming(CountZeros, CallBlock);
1726
1727 // We are explicitly handling the zero case, so we can set the intrinsic's
1728 // undefined zero argument to 'true'. This will also prevent reprocessing the
1729 // intrinsic; we only despeculate when a zero input is defined.
1730 CountZeros->setArgOperand(1, Builder.getTrue());
1731 ModifiedDT = true;
1732 return true;
1733}
1734
Sanjay Patelfc580a62015-09-21 23:03:16 +00001735bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool& ModifiedDT) {
Chris Lattner7a277142011-01-15 07:14:54 +00001736 BasicBlock *BB = CI->getParent();
Nadav Rotem465834c2012-07-24 10:51:42 +00001737
Chris Lattner7a277142011-01-15 07:14:54 +00001738 // Lower inline assembly if we can.
1739 // If we found an inline asm expession, and if the target knows how to
1740 // lower it to normal LLVM code, do so now.
1741 if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
1742 if (TLI->ExpandInlineAsm(CI)) {
1743 // Avoid invalidating the iterator.
1744 CurInstIterator = BB->begin();
1745 // Avoid processing instructions out of order, which could cause
1746 // reuse before a value is defined.
1747 SunkAddrs.clear();
1748 return true;
1749 }
1750 // Sink address computing for memory operands into the block.
Sanjay Patelfc580a62015-09-21 23:03:16 +00001751 if (optimizeInlineAsmInst(CI))
Chris Lattner7a277142011-01-15 07:14:54 +00001752 return true;
1753 }
Nadav Rotem465834c2012-07-24 10:51:42 +00001754
John Brawn0dbcd652015-03-18 12:01:59 +00001755 // Align the pointer arguments to this call if the target thinks it's a good
1756 // idea
1757 unsigned MinSize, PrefAlign;
Mehdi Amini4fe37982015-07-07 18:45:17 +00001758 if (TLI && TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) {
John Brawn0dbcd652015-03-18 12:01:59 +00001759 for (auto &Arg : CI->arg_operands()) {
1760 // We want to align both objects whose address is used directly and
1761 // objects whose address is used in casts and GEPs, though it only makes
1762 // sense for GEPs if the offset is a multiple of the desired alignment and
1763 // if size - offset meets the size threshold.
1764 if (!Arg->getType()->isPointerTy())
1765 continue;
Mehdi Amini4fe37982015-07-07 18:45:17 +00001766 APInt Offset(DL->getPointerSizeInBits(
1767 cast<PointerType>(Arg->getType())->getAddressSpace()),
1768 0);
1769 Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*DL, Offset);
John Brawn0dbcd652015-03-18 12:01:59 +00001770 uint64_t Offset2 = Offset.getLimitedValue();
John Brawne8fd6c82015-04-13 10:47:39 +00001771 if ((Offset2 & (PrefAlign-1)) != 0)
1772 continue;
John Brawn0dbcd652015-03-18 12:01:59 +00001773 AllocaInst *AI;
Mehdi Amini4fe37982015-07-07 18:45:17 +00001774 if ((AI = dyn_cast<AllocaInst>(Val)) && AI->getAlignment() < PrefAlign &&
1775 DL->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2)
John Brawn0dbcd652015-03-18 12:01:59 +00001776 AI->setAlignment(PrefAlign);
John Brawne8fd6c82015-04-13 10:47:39 +00001777 // Global variables can only be aligned if they are defined in this
1778 // object (i.e. they are uniquely initialized in this object), and
1779 // over-aligning global variables that have an explicit section is
1780 // forbidden.
1781 GlobalVariable *GV;
James Y Knightac03dca2016-01-15 16:33:06 +00001782 if ((GV = dyn_cast<GlobalVariable>(Val)) && GV->canIncreaseAlignment() &&
1783 GV->getAlignment() < PrefAlign &&
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00001784 DL->getTypeAllocSize(GV->getValueType()) >=
Mehdi Amini4fe37982015-07-07 18:45:17 +00001785 MinSize + Offset2)
John Brawne8fd6c82015-04-13 10:47:39 +00001786 GV->setAlignment(PrefAlign);
John Brawn0dbcd652015-03-18 12:01:59 +00001787 }
1788 // If this is a memcpy (or similar) then we may be able to improve the
1789 // alignment
1790 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) {
Mehdi Amini4fe37982015-07-07 18:45:17 +00001791 unsigned Align = getKnownAlignment(MI->getDest(), *DL);
John Brawn0dbcd652015-03-18 12:01:59 +00001792 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Mehdi Amini4fe37982015-07-07 18:45:17 +00001793 Align = std::min(Align, getKnownAlignment(MTI->getSource(), *DL));
Pete Cooper67cf9a72015-11-19 05:56:52 +00001794 if (Align > MI->getAlignment())
1795 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), Align));
John Brawn0dbcd652015-03-18 12:01:59 +00001796 }
1797 }
1798
Philip Reamesac115ed2016-03-09 23:13:12 +00001799 // If we have a cold call site, try to sink addressing computation into the
1800 // cold block. This interacts with our handling for loads and stores to
1801 // ensure that we can fold all uses of a potential addressing computation
1802 // into their uses. TODO: generalize this to work over profiling data
1803 if (!OptSize && CI->hasFnAttr(Attribute::Cold))
1804 for (auto &Arg : CI->arg_operands()) {
1805 if (!Arg->getType()->isPointerTy())
1806 continue;
1807 unsigned AS = Arg->getType()->getPointerAddressSpace();
1808 return optimizeMemoryInst(CI, Arg, Arg->getType(), AS);
1809 }
Junmo Park6098cbb2016-03-11 07:05:32 +00001810
Eric Christopher4b7948e2010-03-11 02:41:03 +00001811 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001812 if (II) {
1813 switch (II->getIntrinsicID()) {
1814 default: break;
1815 case Intrinsic::objectsize: {
1816 // Lower all uses of llvm.objectsize.*
Petar Jovanovic644b8c12016-04-13 12:25:25 +00001817 uint64_t Size;
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001818 Type *ReturnTy = CI->getType();
Petar Jovanovic644b8c12016-04-13 12:25:25 +00001819 Constant *RetVal = nullptr;
1820 ConstantInt *Op1 = cast<ConstantInt>(II->getArgOperand(1));
1821 ObjSizeMode Mode = Op1->isZero() ? ObjSizeMode::Max : ObjSizeMode::Min;
1822 if (getObjectSize(II->getArgOperand(0),
1823 Size, *DL, TLInfo, false, Mode)) {
1824 RetVal = ConstantInt::get(ReturnTy, Size);
1825 } else {
1826 RetVal = ConstantInt::get(ReturnTy,
1827 Mode == ObjSizeMode::Min ? 0 : -1ULL);
1828 }
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001829 // Substituting this can cause recursive simplifications, which can
1830 // invalidate our iterator. Use a WeakVH to hold onto it in case this
1831 // happens.
Duncan P. N. Exon Smith7b269642016-02-21 19:37:45 +00001832 Value *CurValue = &*CurInstIterator;
1833 WeakVH IterHandle(CurValue);
Nadav Rotem465834c2012-07-24 10:51:42 +00001834
Sanjay Patel545a4562016-01-20 18:59:16 +00001835 replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
Chris Lattner1b93be52011-01-15 07:25:29 +00001836
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001837 // If the iterator instruction was recursively deleted, start over at the
1838 // start of the block.
Duncan P. N. Exon Smith7b269642016-02-21 19:37:45 +00001839 if (IterHandle != CurValue) {
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001840 CurInstIterator = BB->begin();
1841 SunkAddrs.clear();
1842 }
1843 return true;
Chris Lattner86d56c62011-01-18 20:53:04 +00001844 }
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001845 case Intrinsic::masked_load: {
1846 // Scalarize unsupported vector masked load
Elena Demikhovsky20662e32015-10-19 07:43:38 +00001847 if (!TTI->isLegalMaskedLoad(CI->getType())) {
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001848 scalarizeMaskedLoad(CI);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001849 ModifiedDT = true;
1850 return true;
1851 }
1852 return false;
1853 }
1854 case Intrinsic::masked_store: {
Elena Demikhovsky20662e32015-10-19 07:43:38 +00001855 if (!TTI->isLegalMaskedStore(CI->getArgOperand(0)->getType())) {
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001856 scalarizeMaskedStore(CI);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001857 ModifiedDT = true;
1858 return true;
1859 }
1860 return false;
1861 }
Elena Demikhovsky09285852015-10-25 15:37:55 +00001862 case Intrinsic::masked_gather: {
1863 if (!TTI->isLegalMaskedGather(CI->getType())) {
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001864 scalarizeMaskedGather(CI);
Elena Demikhovsky09285852015-10-25 15:37:55 +00001865 ModifiedDT = true;
1866 return true;
1867 }
1868 return false;
1869 }
1870 case Intrinsic::masked_scatter: {
1871 if (!TTI->isLegalMaskedScatter(CI->getArgOperand(0)->getType())) {
Sanjay Patel3388d1f2016-01-22 21:11:47 +00001872 scalarizeMaskedScatter(CI);
Elena Demikhovsky09285852015-10-25 15:37:55 +00001873 ModifiedDT = true;
1874 return true;
1875 }
1876 return false;
1877 }
Ahmed Bougacha236f9042015-05-22 21:37:17 +00001878 case Intrinsic::aarch64_stlxr:
1879 case Intrinsic::aarch64_stxr: {
1880 ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0));
1881 if (!ExtVal || !ExtVal->hasOneUse() ||
1882 ExtVal->getParent() == CI->getParent())
1883 return false;
1884 // Sink a zext feeding stlxr/stxr before it, so it can be folded into it.
1885 ExtVal->moveBefore(CI);
Ahmed Bougachaf3299142015-06-17 20:44:32 +00001886 // Mark this instruction as "inserted by CGP", so that other
1887 // optimizations don't touch it.
1888 InsertedInsts.insert(ExtVal);
Ahmed Bougacha236f9042015-05-22 21:37:17 +00001889 return true;
1890 }
Piotr Padlewski6c15ec42015-09-15 18:32:14 +00001891 case Intrinsic::invariant_group_barrier:
1892 II->replaceAllUsesWith(II->getArgOperand(0));
1893 II->eraseFromParent();
1894 return true;
Sanjay Patel4699b8a2015-11-19 16:37:10 +00001895
1896 case Intrinsic::cttz:
1897 case Intrinsic::ctlz:
1898 // If counting zeros is expensive, try to avoid it.
1899 return despeculateCountZeros(II, TLI, DL, ModifiedDT);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001900 }
Eric Christopher4b7948e2010-03-11 02:41:03 +00001901
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001902 if (TLI) {
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00001903 // Unknown address space.
1904 // TODO: Target hook to pick which address space the intrinsic cares
1905 // about?
1906 unsigned AddrSpace = ~0u;
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001907 SmallVector<Value*, 2> PtrOps;
1908 Type *AccessTy;
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00001909 if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy, AddrSpace))
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001910 while (!PtrOps.empty())
Sanjay Patelfc580a62015-09-21 23:03:16 +00001911 if (optimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy, AddrSpace))
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001912 return true;
1913 }
Pete Cooper615fd892012-03-13 20:59:56 +00001914 }
1915
Eric Christopher4b7948e2010-03-11 02:41:03 +00001916 // From here on out we're working with named functions.
Craig Topperc0196b12014-04-14 00:51:57 +00001917 if (!CI->getCalledFunction()) return false;
Devang Patel0da52502011-05-26 21:51:06 +00001918
Benjamin Kramer7b88a492010-03-12 09:27:41 +00001919 // Lower all default uses of _chk calls. This is very similar
1920 // to what InstCombineCalls does, but here we are only lowering calls
Ahmed Bougachae03bef72015-01-12 17:22:43 +00001921 // to fortified library functions (e.g. __memcpy_chk) that have the default
1922 // "don't know" as the objectsize. Anything else should be left alone.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001923 FortifiedLibCallSimplifier Simplifier(TLInfo, true);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00001924 if (Value *V = Simplifier.optimizeCall(CI)) {
1925 CI->replaceAllUsesWith(V);
1926 CI->eraseFromParent();
1927 return true;
1928 }
1929 return false;
Eric Christopher4b7948e2010-03-11 02:41:03 +00001930}
Chris Lattner1b93be52011-01-15 07:25:29 +00001931
Sanjay Patel4ac6b112015-09-21 22:47:23 +00001932/// Look for opportunities to duplicate return instructions to the predecessor
1933/// to enable tail call optimizations. The case it is currently looking for is:
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001934/// @code
Evan Cheng0663f232011-03-21 01:19:09 +00001935/// bb0:
1936/// %tmp0 = tail call i32 @f0()
1937/// br label %return
1938/// bb1:
1939/// %tmp1 = tail call i32 @f1()
1940/// br label %return
1941/// bb2:
1942/// %tmp2 = tail call i32 @f2()
1943/// br label %return
1944/// return:
1945/// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
1946/// ret i32 %retval
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001947/// @endcode
Evan Cheng0663f232011-03-21 01:19:09 +00001948///
1949/// =>
1950///
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001951/// @code
Evan Cheng0663f232011-03-21 01:19:09 +00001952/// bb0:
1953/// %tmp0 = tail call i32 @f0()
1954/// ret i32 %tmp0
1955/// bb1:
1956/// %tmp1 = tail call i32 @f1()
1957/// ret i32 %tmp1
1958/// bb2:
1959/// %tmp2 = tail call i32 @f2()
1960/// ret i32 %tmp2
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001961/// @endcode
Sanjay Patelfc580a62015-09-21 23:03:16 +00001962bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB) {
Cameron Zwarich47e71752011-03-24 04:51:51 +00001963 if (!TLI)
1964 return false;
1965
Benjamin Kramer455fa352012-11-23 19:17:06 +00001966 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
1967 if (!RI)
1968 return false;
1969
Craig Topperc0196b12014-04-14 00:51:57 +00001970 PHINode *PN = nullptr;
1971 BitCastInst *BCI = nullptr;
Evan Cheng0663f232011-03-21 01:19:09 +00001972 Value *V = RI->getReturnValue();
Evan Cheng249716e2012-07-27 21:21:26 +00001973 if (V) {
1974 BCI = dyn_cast<BitCastInst>(V);
1975 if (BCI)
1976 V = BCI->getOperand(0);
1977
1978 PN = dyn_cast<PHINode>(V);
1979 if (!PN)
1980 return false;
1981 }
Evan Cheng0663f232011-03-21 01:19:09 +00001982
Cameron Zwarich4649f172011-03-24 04:52:10 +00001983 if (PN && PN->getParent() != BB)
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001984 return false;
Evan Cheng0663f232011-03-21 01:19:09 +00001985
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001986 // It's not safe to eliminate the sign / zero extension of the return value.
1987 // See llvm::isInTailCallPosition().
1988 const Function *F = BB->getParent();
Bill Wendling658d24d2013-01-18 21:53:16 +00001989 AttributeSet CallerAttrs = F->getAttributes();
1990 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
1991 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001992 return false;
Evan Cheng0663f232011-03-21 01:19:09 +00001993
Cameron Zwarich4649f172011-03-24 04:52:10 +00001994 // Make sure there are no instructions between the PHI and return, or that the
1995 // return is the first instruction in the block.
1996 if (PN) {
1997 BasicBlock::iterator BI = BB->begin();
1998 do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
Evan Cheng249716e2012-07-27 21:21:26 +00001999 if (&*BI == BCI)
2000 // Also skip over the bitcast.
2001 ++BI;
Cameron Zwarich4649f172011-03-24 04:52:10 +00002002 if (&*BI != RI)
2003 return false;
2004 } else {
Cameron Zwarich74157ab2011-03-24 16:34:59 +00002005 BasicBlock::iterator BI = BB->begin();
2006 while (isa<DbgInfoIntrinsic>(BI)) ++BI;
2007 if (&*BI != RI)
Cameron Zwarich4649f172011-03-24 04:52:10 +00002008 return false;
2009 }
Evan Cheng0663f232011-03-21 01:19:09 +00002010
Cameron Zwarich0e331c02011-03-24 04:52:07 +00002011 /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
2012 /// call.
2013 SmallVector<CallInst*, 4> TailCalls;
Cameron Zwarich4649f172011-03-24 04:52:10 +00002014 if (PN) {
2015 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
2016 CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
2017 // Make sure the phi value is indeed produced by the tail call.
2018 if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
2019 TLI->mayBeEmittedAsTailCall(CI))
2020 TailCalls.push_back(CI);
2021 }
2022 } else {
2023 SmallPtrSet<BasicBlock*, 4> VisitedBBs;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00002024 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
David Blaikie70573dc2014-11-19 07:49:26 +00002025 if (!VisitedBBs.insert(*PI).second)
Cameron Zwarich4649f172011-03-24 04:52:10 +00002026 continue;
2027
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00002028 BasicBlock::InstListType &InstList = (*PI)->getInstList();
Cameron Zwarich4649f172011-03-24 04:52:10 +00002029 BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
2030 BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
Cameron Zwarich74157ab2011-03-24 16:34:59 +00002031 do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
2032 if (RI == RE)
Cameron Zwarich4649f172011-03-24 04:52:10 +00002033 continue;
Cameron Zwarich74157ab2011-03-24 16:34:59 +00002034
Cameron Zwarich4649f172011-03-24 04:52:10 +00002035 CallInst *CI = dyn_cast<CallInst>(&*RI);
Cameron Zwarich2edfe772011-03-24 15:54:11 +00002036 if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI))
Cameron Zwarich4649f172011-03-24 04:52:10 +00002037 TailCalls.push_back(CI);
2038 }
Evan Cheng0663f232011-03-21 01:19:09 +00002039 }
2040
Cameron Zwarich0e331c02011-03-24 04:52:07 +00002041 bool Changed = false;
2042 for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
2043 CallInst *CI = TailCalls[i];
2044 CallSite CS(CI);
2045
2046 // Conservatively require the attributes of the call to match those of the
2047 // return. Ignore noalias because it doesn't affect the call sequence.
Bill Wendling658d24d2013-01-18 21:53:16 +00002048 AttributeSet CalleeAttrs = CS.getAttributes();
2049 if (AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex).
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002050 removeAttribute(Attribute::NoAlias) !=
Bill Wendling658d24d2013-01-18 21:53:16 +00002051 AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex).
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002052 removeAttribute(Attribute::NoAlias))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00002053 continue;
2054
2055 // Make sure the call instruction is followed by an unconditional branch to
2056 // the return block.
2057 BasicBlock *CallBB = CI->getParent();
2058 BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
2059 if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
2060 continue;
2061
2062 // Duplicate the return into CallBB.
2063 (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
Devang Patel8f606d72011-03-24 15:35:25 +00002064 ModifiedDT = Changed = true;
Cameron Zwarich0e331c02011-03-24 04:52:07 +00002065 ++NumRetsDup;
2066 }
2067
2068 // If we eliminated all predecessors of the block, delete the block now.
Evan Cheng64a223a2012-09-28 23:58:57 +00002069 if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00002070 BB->eraseFromParent();
2071
2072 return Changed;
Evan Cheng0663f232011-03-21 01:19:09 +00002073}
2074
Chris Lattner728f9022008-11-25 07:09:13 +00002075//===----------------------------------------------------------------------===//
Chris Lattner728f9022008-11-25 07:09:13 +00002076// Memory Optimization
2077//===----------------------------------------------------------------------===//
2078
Chandler Carruthc8925912013-01-05 02:09:22 +00002079namespace {
2080
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002081/// This is an extended version of TargetLowering::AddrMode
Chandler Carruthc8925912013-01-05 02:09:22 +00002082/// which holds actual Value*'s for register values.
Chandler Carruth95f83e02013-01-07 15:14:13 +00002083struct ExtAddrMode : public TargetLowering::AddrMode {
Chandler Carruthc8925912013-01-05 02:09:22 +00002084 Value *BaseReg;
2085 Value *ScaledReg;
Craig Topperc0196b12014-04-14 00:51:57 +00002086 ExtAddrMode() : BaseReg(nullptr), ScaledReg(nullptr) {}
Chandler Carruthc8925912013-01-05 02:09:22 +00002087 void print(raw_ostream &OS) const;
2088 void dump() const;
Stephen Lin837bba12013-07-15 17:55:02 +00002089
Chandler Carruthc8925912013-01-05 02:09:22 +00002090 bool operator==(const ExtAddrMode& O) const {
2091 return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) &&
2092 (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) &&
2093 (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale);
2094 }
2095};
2096
Eli Friedmanc1f1f852013-09-10 23:09:24 +00002097#ifndef NDEBUG
2098static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
2099 AM.print(OS);
2100 return OS;
2101}
2102#endif
2103
Chandler Carruthc8925912013-01-05 02:09:22 +00002104void ExtAddrMode::print(raw_ostream &OS) const {
2105 bool NeedPlus = false;
2106 OS << "[";
2107 if (BaseGV) {
2108 OS << (NeedPlus ? " + " : "")
2109 << "GV:";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002110 BaseGV->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00002111 NeedPlus = true;
2112 }
2113
Richard Trieuc0f91212014-05-30 03:15:17 +00002114 if (BaseOffs) {
2115 OS << (NeedPlus ? " + " : "")
2116 << BaseOffs;
2117 NeedPlus = true;
2118 }
Chandler Carruthc8925912013-01-05 02:09:22 +00002119
2120 if (BaseReg) {
2121 OS << (NeedPlus ? " + " : "")
2122 << "Base:";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002123 BaseReg->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00002124 NeedPlus = true;
2125 }
2126 if (Scale) {
2127 OS << (NeedPlus ? " + " : "")
2128 << Scale << "*";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002129 ScaledReg->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00002130 }
2131
2132 OS << ']';
2133}
2134
2135#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +00002136LLVM_DUMP_METHOD void ExtAddrMode::dump() const {
Chandler Carruthc8925912013-01-05 02:09:22 +00002137 print(dbgs());
2138 dbgs() << '\n';
2139}
2140#endif
2141
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002142/// \brief This class provides transaction based operation on the IR.
2143/// Every change made through this class is recorded in the internal state and
2144/// can be undone (rollback) until commit is called.
2145class TypePromotionTransaction {
2146
2147 /// \brief This represents the common interface of the individual transaction.
2148 /// Each class implements the logic for doing one specific modification on
2149 /// the IR via the TypePromotionTransaction.
2150 class TypePromotionAction {
2151 protected:
2152 /// The Instruction modified.
2153 Instruction *Inst;
2154
2155 public:
2156 /// \brief Constructor of the action.
2157 /// The constructor performs the related action on the IR.
2158 TypePromotionAction(Instruction *Inst) : Inst(Inst) {}
2159
2160 virtual ~TypePromotionAction() {}
2161
2162 /// \brief Undo the modification done by this action.
2163 /// When this method is called, the IR must be in the same state as it was
2164 /// before this action was applied.
2165 /// \pre Undoing the action works if and only if the IR is in the exact same
2166 /// state as it was directly after this action was applied.
2167 virtual void undo() = 0;
2168
2169 /// \brief Advocate every change made by this action.
2170 /// When the results on the IR of the action are to be kept, it is important
2171 /// to call this function, otherwise hidden information may be kept forever.
2172 virtual void commit() {
2173 // Nothing to be done, this action is not doing anything.
2174 }
2175 };
2176
2177 /// \brief Utility to remember the position of an instruction.
2178 class InsertionHandler {
2179 /// Position of an instruction.
2180 /// Either an instruction:
2181 /// - Is the first in a basic block: BB is used.
2182 /// - Has a previous instructon: PrevInst is used.
2183 union {
2184 Instruction *PrevInst;
2185 BasicBlock *BB;
2186 } Point;
2187 /// Remember whether or not the instruction had a previous instruction.
2188 bool HasPrevInstruction;
2189
2190 public:
2191 /// \brief Record the position of \p Inst.
2192 InsertionHandler(Instruction *Inst) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00002193 BasicBlock::iterator It = Inst->getIterator();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002194 HasPrevInstruction = (It != (Inst->getParent()->begin()));
2195 if (HasPrevInstruction)
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00002196 Point.PrevInst = &*--It;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002197 else
2198 Point.BB = Inst->getParent();
2199 }
2200
2201 /// \brief Insert \p Inst at the recorded position.
2202 void insert(Instruction *Inst) {
2203 if (HasPrevInstruction) {
2204 if (Inst->getParent())
2205 Inst->removeFromParent();
2206 Inst->insertAfter(Point.PrevInst);
2207 } else {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00002208 Instruction *Position = &*Point.BB->getFirstInsertionPt();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002209 if (Inst->getParent())
2210 Inst->moveBefore(Position);
2211 else
2212 Inst->insertBefore(Position);
2213 }
2214 }
2215 };
2216
2217 /// \brief Move an instruction before another.
2218 class InstructionMoveBefore : public TypePromotionAction {
2219 /// Original position of the instruction.
2220 InsertionHandler Position;
2221
2222 public:
2223 /// \brief Move \p Inst before \p Before.
2224 InstructionMoveBefore(Instruction *Inst, Instruction *Before)
2225 : TypePromotionAction(Inst), Position(Inst) {
2226 DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before << "\n");
2227 Inst->moveBefore(Before);
2228 }
2229
2230 /// \brief Move the instruction back to its original position.
Craig Topper4584cd52014-03-07 09:26:03 +00002231 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002232 DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n");
2233 Position.insert(Inst);
2234 }
2235 };
2236
2237 /// \brief Set the operand of an instruction with a new value.
2238 class OperandSetter : public TypePromotionAction {
2239 /// Original operand of the instruction.
2240 Value *Origin;
2241 /// Index of the modified instruction.
2242 unsigned Idx;
2243
2244 public:
2245 /// \brief Set \p Idx operand of \p Inst with \p NewVal.
2246 OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal)
2247 : TypePromotionAction(Inst), Idx(Idx) {
2248 DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n"
2249 << "for:" << *Inst << "\n"
2250 << "with:" << *NewVal << "\n");
2251 Origin = Inst->getOperand(Idx);
2252 Inst->setOperand(Idx, NewVal);
2253 }
2254
2255 /// \brief Restore the original value of the instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00002256 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002257 DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n"
2258 << "for: " << *Inst << "\n"
2259 << "with: " << *Origin << "\n");
2260 Inst->setOperand(Idx, Origin);
2261 }
2262 };
2263
2264 /// \brief Hide the operands of an instruction.
2265 /// Do as if this instruction was not using any of its operands.
2266 class OperandsHider : public TypePromotionAction {
2267 /// The list of original operands.
2268 SmallVector<Value *, 4> OriginalValues;
2269
2270 public:
2271 /// \brief Remove \p Inst from the uses of the operands of \p Inst.
2272 OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) {
2273 DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n");
2274 unsigned NumOpnds = Inst->getNumOperands();
2275 OriginalValues.reserve(NumOpnds);
2276 for (unsigned It = 0; It < NumOpnds; ++It) {
2277 // Save the current operand.
2278 Value *Val = Inst->getOperand(It);
2279 OriginalValues.push_back(Val);
2280 // Set a dummy one.
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00002281 // We could use OperandSetter here, but that would imply an overhead
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002282 // that we are not willing to pay.
2283 Inst->setOperand(It, UndefValue::get(Val->getType()));
2284 }
2285 }
2286
2287 /// \brief Restore the original list of uses.
Craig Topper4584cd52014-03-07 09:26:03 +00002288 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002289 DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n");
2290 for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It)
2291 Inst->setOperand(It, OriginalValues[It]);
2292 }
2293 };
2294
2295 /// \brief Build a truncate instruction.
2296 class TruncBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00002297 Value *Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002298 public:
2299 /// \brief Build a truncate instruction of \p Opnd producing a \p Ty
2300 /// result.
2301 /// trunc Opnd to Ty.
2302 TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) {
2303 IRBuilder<> Builder(Opnd);
Quentin Colombetac55b152014-09-16 22:36:07 +00002304 Val = Builder.CreateTrunc(Opnd, Ty, "promoted");
2305 DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n");
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002306 }
2307
Quentin Colombetac55b152014-09-16 22:36:07 +00002308 /// \brief Get the built value.
2309 Value *getBuiltValue() { return Val; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002310
2311 /// \brief Remove the built instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00002312 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00002313 DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n");
2314 if (Instruction *IVal = dyn_cast<Instruction>(Val))
2315 IVal->eraseFromParent();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002316 }
2317 };
2318
2319 /// \brief Build a sign extension instruction.
2320 class SExtBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00002321 Value *Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002322 public:
2323 /// \brief Build a sign extension instruction of \p Opnd producing a \p Ty
2324 /// result.
2325 /// sext Opnd to Ty.
2326 SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
Quentin Colombetac55b152014-09-16 22:36:07 +00002327 : TypePromotionAction(InsertPt) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002328 IRBuilder<> Builder(InsertPt);
Quentin Colombetac55b152014-09-16 22:36:07 +00002329 Val = Builder.CreateSExt(Opnd, Ty, "promoted");
2330 DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n");
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002331 }
2332
Quentin Colombetac55b152014-09-16 22:36:07 +00002333 /// \brief Get the built value.
2334 Value *getBuiltValue() { return Val; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002335
2336 /// \brief Remove the built instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00002337 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00002338 DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n");
2339 if (Instruction *IVal = dyn_cast<Instruction>(Val))
2340 IVal->eraseFromParent();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002341 }
2342 };
2343
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002344 /// \brief Build a zero extension instruction.
2345 class ZExtBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00002346 Value *Val;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002347 public:
2348 /// \brief Build a zero extension instruction of \p Opnd producing a \p Ty
2349 /// result.
2350 /// zext Opnd to Ty.
2351 ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
Quentin Colombetac55b152014-09-16 22:36:07 +00002352 : TypePromotionAction(InsertPt) {
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002353 IRBuilder<> Builder(InsertPt);
Quentin Colombetac55b152014-09-16 22:36:07 +00002354 Val = Builder.CreateZExt(Opnd, Ty, "promoted");
2355 DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n");
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002356 }
2357
Quentin Colombetac55b152014-09-16 22:36:07 +00002358 /// \brief Get the built value.
2359 Value *getBuiltValue() { return Val; }
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002360
2361 /// \brief Remove the built instruction.
2362 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00002363 DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n");
2364 if (Instruction *IVal = dyn_cast<Instruction>(Val))
2365 IVal->eraseFromParent();
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002366 }
2367 };
2368
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002369 /// \brief Mutate an instruction to another type.
2370 class TypeMutator : public TypePromotionAction {
2371 /// Record the original type.
2372 Type *OrigTy;
2373
2374 public:
2375 /// \brief Mutate the type of \p Inst into \p NewTy.
2376 TypeMutator(Instruction *Inst, Type *NewTy)
2377 : TypePromotionAction(Inst), OrigTy(Inst->getType()) {
2378 DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy
2379 << "\n");
2380 Inst->mutateType(NewTy);
2381 }
2382
2383 /// \brief Mutate the instruction back to its original type.
Craig Topper4584cd52014-03-07 09:26:03 +00002384 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002385 DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy
2386 << "\n");
2387 Inst->mutateType(OrigTy);
2388 }
2389 };
2390
2391 /// \brief Replace the uses of an instruction by another instruction.
2392 class UsesReplacer : public TypePromotionAction {
2393 /// Helper structure to keep track of the replaced uses.
2394 struct InstructionAndIdx {
2395 /// The instruction using the instruction.
2396 Instruction *Inst;
2397 /// The index where this instruction is used for Inst.
2398 unsigned Idx;
2399 InstructionAndIdx(Instruction *Inst, unsigned Idx)
2400 : Inst(Inst), Idx(Idx) {}
2401 };
2402
2403 /// Keep track of the original uses (pair Instruction, Index).
2404 SmallVector<InstructionAndIdx, 4> OriginalUses;
2405 typedef SmallVectorImpl<InstructionAndIdx>::iterator use_iterator;
2406
2407 public:
2408 /// \brief Replace all the use of \p Inst by \p New.
2409 UsesReplacer(Instruction *Inst, Value *New) : TypePromotionAction(Inst) {
2410 DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New
2411 << "\n");
2412 // Record the original uses.
Chandler Carruthcdf47882014-03-09 03:16:01 +00002413 for (Use &U : Inst->uses()) {
2414 Instruction *UserI = cast<Instruction>(U.getUser());
2415 OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo()));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002416 }
2417 // Now, we can replace the uses.
2418 Inst->replaceAllUsesWith(New);
2419 }
2420
2421 /// \brief Reassign the original uses of Inst to Inst.
Craig Topper4584cd52014-03-07 09:26:03 +00002422 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002423 DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n");
2424 for (use_iterator UseIt = OriginalUses.begin(),
2425 EndIt = OriginalUses.end();
2426 UseIt != EndIt; ++UseIt) {
2427 UseIt->Inst->setOperand(UseIt->Idx, Inst);
2428 }
2429 }
2430 };
2431
2432 /// \brief Remove an instruction from the IR.
2433 class InstructionRemover : public TypePromotionAction {
2434 /// Original position of the instruction.
2435 InsertionHandler Inserter;
2436 /// Helper structure to hide all the link to the instruction. In other
2437 /// words, this helps to do as if the instruction was removed.
2438 OperandsHider Hider;
2439 /// Keep track of the uses replaced, if any.
2440 UsesReplacer *Replacer;
2441
2442 public:
2443 /// \brief Remove all reference of \p Inst and optinally replace all its
2444 /// uses with New.
Craig Topperc0196b12014-04-14 00:51:57 +00002445 /// \pre If !Inst->use_empty(), then New != nullptr
2446 InstructionRemover(Instruction *Inst, Value *New = nullptr)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002447 : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst),
Craig Topperc0196b12014-04-14 00:51:57 +00002448 Replacer(nullptr) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002449 if (New)
2450 Replacer = new UsesReplacer(Inst, New);
2451 DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n");
2452 Inst->removeFromParent();
2453 }
2454
Alexander Kornienkof817c1c2015-04-11 02:11:45 +00002455 ~InstructionRemover() override { delete Replacer; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002456
2457 /// \brief Really remove the instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00002458 void commit() override { delete Inst; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002459
2460 /// \brief Resurrect the instruction and reassign it to the proper uses if
2461 /// new value was provided when build this action.
Craig Topper4584cd52014-03-07 09:26:03 +00002462 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002463 DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n");
2464 Inserter.insert(Inst);
2465 if (Replacer)
2466 Replacer->undo();
2467 Hider.undo();
2468 }
2469 };
2470
2471public:
2472 /// Restoration point.
2473 /// The restoration point is a pointer to an action instead of an iterator
2474 /// because the iterator may be invalidated but not the pointer.
2475 typedef const TypePromotionAction *ConstRestorationPt;
2476 /// Advocate every changes made in that transaction.
2477 void commit();
2478 /// Undo all the changes made after the given point.
2479 void rollback(ConstRestorationPt Point);
2480 /// Get the current restoration point.
2481 ConstRestorationPt getRestorationPoint() const;
2482
2483 /// \name API for IR modification with state keeping to support rollback.
2484 /// @{
2485 /// Same as Instruction::setOperand.
2486 void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal);
2487 /// Same as Instruction::eraseFromParent.
Craig Topperc0196b12014-04-14 00:51:57 +00002488 void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002489 /// Same as Value::replaceAllUsesWith.
2490 void replaceAllUsesWith(Instruction *Inst, Value *New);
2491 /// Same as Value::mutateType.
2492 void mutateType(Instruction *Inst, Type *NewTy);
2493 /// Same as IRBuilder::createTrunc.
Quentin Colombetac55b152014-09-16 22:36:07 +00002494 Value *createTrunc(Instruction *Opnd, Type *Ty);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002495 /// Same as IRBuilder::createSExt.
Quentin Colombetac55b152014-09-16 22:36:07 +00002496 Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty);
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002497 /// Same as IRBuilder::createZExt.
Quentin Colombetac55b152014-09-16 22:36:07 +00002498 Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002499 /// Same as Instruction::moveBefore.
2500 void moveBefore(Instruction *Inst, Instruction *Before);
2501 /// @}
2502
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002503private:
2504 /// The ordered list of actions made so far.
David Blaikie7620b312014-04-15 06:17:44 +00002505 SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions;
2506 typedef SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator CommitPt;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002507};
2508
2509void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx,
2510 Value *NewVal) {
2511 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002512 make_unique<TypePromotionTransaction::OperandSetter>(Inst, Idx, NewVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002513}
2514
2515void TypePromotionTransaction::eraseInstruction(Instruction *Inst,
2516 Value *NewVal) {
2517 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002518 make_unique<TypePromotionTransaction::InstructionRemover>(Inst, NewVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002519}
2520
2521void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst,
2522 Value *New) {
David Blaikie7620b312014-04-15 06:17:44 +00002523 Actions.push_back(make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002524}
2525
2526void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) {
David Blaikie7620b312014-04-15 06:17:44 +00002527 Actions.push_back(make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002528}
2529
Quentin Colombetac55b152014-09-16 22:36:07 +00002530Value *TypePromotionTransaction::createTrunc(Instruction *Opnd,
2531 Type *Ty) {
David Blaikie7620b312014-04-15 06:17:44 +00002532 std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002533 Value *Val = Ptr->getBuiltValue();
David Blaikie7620b312014-04-15 06:17:44 +00002534 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002535 return Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002536}
2537
Quentin Colombetac55b152014-09-16 22:36:07 +00002538Value *TypePromotionTransaction::createSExt(Instruction *Inst,
2539 Value *Opnd, Type *Ty) {
David Blaikie7620b312014-04-15 06:17:44 +00002540 std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002541 Value *Val = Ptr->getBuiltValue();
David Blaikie7620b312014-04-15 06:17:44 +00002542 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002543 return Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002544}
2545
Quentin Colombetac55b152014-09-16 22:36:07 +00002546Value *TypePromotionTransaction::createZExt(Instruction *Inst,
2547 Value *Opnd, Type *Ty) {
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002548 std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002549 Value *Val = Ptr->getBuiltValue();
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002550 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002551 return Val;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002552}
2553
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002554void TypePromotionTransaction::moveBefore(Instruction *Inst,
2555 Instruction *Before) {
2556 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002557 make_unique<TypePromotionTransaction::InstructionMoveBefore>(Inst, Before));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002558}
2559
2560TypePromotionTransaction::ConstRestorationPt
2561TypePromotionTransaction::getRestorationPoint() const {
David Blaikie7620b312014-04-15 06:17:44 +00002562 return !Actions.empty() ? Actions.back().get() : nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002563}
2564
2565void TypePromotionTransaction::commit() {
2566 for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt;
David Blaikie7620b312014-04-15 06:17:44 +00002567 ++It)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002568 (*It)->commit();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002569 Actions.clear();
2570}
2571
2572void TypePromotionTransaction::rollback(
2573 TypePromotionTransaction::ConstRestorationPt Point) {
David Blaikie7620b312014-04-15 06:17:44 +00002574 while (!Actions.empty() && Point != Actions.back().get()) {
2575 std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002576 Curr->undo();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002577 }
2578}
2579
Chandler Carruthc8925912013-01-05 02:09:22 +00002580/// \brief A helper class for matching addressing modes.
2581///
2582/// This encapsulates the logic for matching the target-legal addressing modes.
2583class AddressingModeMatcher {
2584 SmallVectorImpl<Instruction*> &AddrModeInsts;
Eric Christopherd75c00c2015-02-26 22:38:34 +00002585 const TargetMachine &TM;
Chandler Carruthc8925912013-01-05 02:09:22 +00002586 const TargetLowering &TLI;
Mehdi Amini4fe37982015-07-07 18:45:17 +00002587 const DataLayout &DL;
Chandler Carruthc8925912013-01-05 02:09:22 +00002588
2589 /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
2590 /// the memory instruction that we're computing this address for.
2591 Type *AccessTy;
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00002592 unsigned AddrSpace;
Chandler Carruthc8925912013-01-05 02:09:22 +00002593 Instruction *MemoryInst;
Stephen Lin837bba12013-07-15 17:55:02 +00002594
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002595 /// This is the addressing mode that we're building up. This is
Chandler Carruthc8925912013-01-05 02:09:22 +00002596 /// part of the return value of this addressing mode matching stuff.
2597 ExtAddrMode &AddrMode;
Stephen Lin837bba12013-07-15 17:55:02 +00002598
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002599 /// The instructions inserted by other CodeGenPrepare optimizations.
2600 const SetOfInstrs &InsertedInsts;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002601 /// A map from the instructions to their type before promotion.
2602 InstrToOrigTy &PromotedInsts;
2603 /// The ongoing transaction where every action should be registered.
2604 TypePromotionTransaction &TPT;
2605
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002606 /// This is set to true when we should not do profitability checks.
2607 /// When true, IsProfitableToFoldIntoAddressingMode always returns true.
Chandler Carruthc8925912013-01-05 02:09:22 +00002608 bool IgnoreProfitability;
Stephen Lin837bba12013-07-15 17:55:02 +00002609
Eric Christopherd75c00c2015-02-26 22:38:34 +00002610 AddressingModeMatcher(SmallVectorImpl<Instruction *> &AMI,
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00002611 const TargetMachine &TM, Type *AT, unsigned AS,
2612 Instruction *MI, ExtAddrMode &AM,
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002613 const SetOfInstrs &InsertedInsts,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002614 InstrToOrigTy &PromotedInsts,
2615 TypePromotionTransaction &TPT)
Eric Christopherd75c00c2015-02-26 22:38:34 +00002616 : AddrModeInsts(AMI), TM(TM),
2617 TLI(*TM.getSubtargetImpl(*MI->getParent()->getParent())
2618 ->getTargetLowering()),
Mehdi Amini4fe37982015-07-07 18:45:17 +00002619 DL(MI->getModule()->getDataLayout()), AccessTy(AT), AddrSpace(AS),
2620 MemoryInst(MI), AddrMode(AM), InsertedInsts(InsertedInsts),
2621 PromotedInsts(PromotedInsts), TPT(TPT) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002622 IgnoreProfitability = false;
2623 }
2624public:
Stephen Lin837bba12013-07-15 17:55:02 +00002625
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002626 /// Find the maximal addressing mode that a load/store of V can fold,
Chandler Carruthc8925912013-01-05 02:09:22 +00002627 /// give an access type of AccessTy. This returns a list of involved
2628 /// instructions in AddrModeInsts.
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002629 /// \p InsertedInsts The instructions inserted by other CodeGenPrepare
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002630 /// optimizations.
2631 /// \p PromotedInsts maps the instructions to their type before promotion.
2632 /// \p The ongoing transaction where every action should be registered.
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00002633 static ExtAddrMode Match(Value *V, Type *AccessTy, unsigned AS,
Chandler Carruthc8925912013-01-05 02:09:22 +00002634 Instruction *MemoryInst,
2635 SmallVectorImpl<Instruction*> &AddrModeInsts,
Eric Christopherd75c00c2015-02-26 22:38:34 +00002636 const TargetMachine &TM,
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002637 const SetOfInstrs &InsertedInsts,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002638 InstrToOrigTy &PromotedInsts,
2639 TypePromotionTransaction &TPT) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002640 ExtAddrMode Result;
2641
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00002642 bool Success = AddressingModeMatcher(AddrModeInsts, TM, AccessTy, AS,
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002643 MemoryInst, Result, InsertedInsts,
Sanjay Patelfc580a62015-09-21 23:03:16 +00002644 PromotedInsts, TPT).matchAddr(V, 0);
Chandler Carruthc8925912013-01-05 02:09:22 +00002645 (void)Success; assert(Success && "Couldn't select *anything*?");
2646 return Result;
2647 }
2648private:
Sanjay Patelfc580a62015-09-21 23:03:16 +00002649 bool matchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
2650 bool matchAddr(Value *V, unsigned Depth);
2651 bool matchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth,
Craig Topperc0196b12014-04-14 00:51:57 +00002652 bool *MovedAway = nullptr);
Sanjay Patelfc580a62015-09-21 23:03:16 +00002653 bool isProfitableToFoldIntoAddressingMode(Instruction *I,
Chandler Carruthc8925912013-01-05 02:09:22 +00002654 ExtAddrMode &AMBefore,
2655 ExtAddrMode &AMAfter);
Sanjay Patelfc580a62015-09-21 23:03:16 +00002656 bool valueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
2657 bool isPromotionProfitable(unsigned NewCost, unsigned OldCost,
Quentin Colombet867c5502014-02-14 22:23:22 +00002658 Value *PromotedOperand) const;
Chandler Carruthc8925912013-01-05 02:09:22 +00002659};
2660
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002661/// Try adding ScaleReg*Scale to the current addressing mode.
Chandler Carruthc8925912013-01-05 02:09:22 +00002662/// Return true and update AddrMode if this addr mode is legal for the target,
2663/// false if not.
Sanjay Patelfc580a62015-09-21 23:03:16 +00002664bool AddressingModeMatcher::matchScaledValue(Value *ScaleReg, int64_t Scale,
Chandler Carruthc8925912013-01-05 02:09:22 +00002665 unsigned Depth) {
2666 // If Scale is 1, then this is the same as adding ScaleReg to the addressing
2667 // mode. Just process that directly.
2668 if (Scale == 1)
Sanjay Patelfc580a62015-09-21 23:03:16 +00002669 return matchAddr(ScaleReg, Depth);
Stephen Lin837bba12013-07-15 17:55:02 +00002670
Chandler Carruthc8925912013-01-05 02:09:22 +00002671 // If the scale is 0, it takes nothing to add this.
2672 if (Scale == 0)
2673 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00002674
Chandler Carruthc8925912013-01-05 02:09:22 +00002675 // If we already have a scale of this value, we can add to it, otherwise, we
2676 // need an available scale field.
2677 if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
2678 return false;
2679
2680 ExtAddrMode TestAddrMode = AddrMode;
2681
2682 // Add scale to turn X*4+X*3 -> X*7. This could also do things like
2683 // [A+B + A*7] -> [B+A*8].
2684 TestAddrMode.Scale += Scale;
2685 TestAddrMode.ScaledReg = ScaleReg;
2686
2687 // If the new address isn't legal, bail out.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002688 if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace))
Chandler Carruthc8925912013-01-05 02:09:22 +00002689 return false;
2690
2691 // It was legal, so commit it.
2692 AddrMode = TestAddrMode;
Stephen Lin837bba12013-07-15 17:55:02 +00002693
Chandler Carruthc8925912013-01-05 02:09:22 +00002694 // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now
2695 // to see if ScaleReg is actually X+C. If so, we can turn this into adding
2696 // X*Scale + C*Scale to addr mode.
Craig Topperc0196b12014-04-14 00:51:57 +00002697 ConstantInt *CI = nullptr; Value *AddLHS = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00002698 if (isa<Instruction>(ScaleReg) && // not a constant expr.
2699 match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) {
2700 TestAddrMode.ScaledReg = AddLHS;
2701 TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
Stephen Lin837bba12013-07-15 17:55:02 +00002702
Chandler Carruthc8925912013-01-05 02:09:22 +00002703 // If this addressing mode is legal, commit it and remember that we folded
2704 // this instruction.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002705 if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002706 AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
2707 AddrMode = TestAddrMode;
2708 return true;
2709 }
2710 }
2711
2712 // Otherwise, not (x+c)*scale, just return what we have.
2713 return true;
2714}
2715
Sanjay Patel4ac6b112015-09-21 22:47:23 +00002716/// This is a little filter, which returns true if an addressing computation
2717/// involving I might be folded into a load/store accessing it.
2718/// This doesn't need to be perfect, but needs to accept at least
Chandler Carruthc8925912013-01-05 02:09:22 +00002719/// the set of instructions that MatchOperationAddr can.
2720static bool MightBeFoldableInst(Instruction *I) {
2721 switch (I->getOpcode()) {
2722 case Instruction::BitCast:
Eli Benderskyf13a0562014-05-22 00:02:52 +00002723 case Instruction::AddrSpaceCast:
Chandler Carruthc8925912013-01-05 02:09:22 +00002724 // Don't touch identity bitcasts.
2725 if (I->getType() == I->getOperand(0)->getType())
2726 return false;
2727 return I->getType()->isPointerTy() || I->getType()->isIntegerTy();
2728 case Instruction::PtrToInt:
2729 // PtrToInt is always a noop, as we know that the int type is pointer sized.
2730 return true;
2731 case Instruction::IntToPtr:
2732 // We know the input is intptr_t, so this is foldable.
2733 return true;
2734 case Instruction::Add:
2735 return true;
2736 case Instruction::Mul:
2737 case Instruction::Shl:
2738 // Can only handle X*C and X << C.
2739 return isa<ConstantInt>(I->getOperand(1));
2740 case Instruction::GetElementPtr:
2741 return true;
2742 default:
2743 return false;
2744 }
2745}
2746
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002747/// \brief Check whether or not \p Val is a legal instruction for \p TLI.
2748/// \note \p Val is assumed to be the product of some type promotion.
2749/// Therefore if \p Val has an undefined state in \p TLI, this is assumed
2750/// to be legal, as the non-promoted value would have had the same state.
Mehdi Amini44ede332015-07-09 02:09:04 +00002751static bool isPromotedInstructionLegal(const TargetLowering &TLI,
2752 const DataLayout &DL, Value *Val) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002753 Instruction *PromotedInst = dyn_cast<Instruction>(Val);
2754 if (!PromotedInst)
2755 return false;
2756 int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode());
2757 // If the ISDOpcode is undefined, it was undefined before the promotion.
2758 if (!ISDOpcode)
2759 return true;
2760 // Otherwise, check if the promoted instruction is legal or not.
2761 return TLI.isOperationLegalOrCustom(
Mehdi Amini44ede332015-07-09 02:09:04 +00002762 ISDOpcode, TLI.getValueType(DL, PromotedInst->getType()));
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002763}
2764
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002765/// \brief Hepler class to perform type promotion.
2766class TypePromotionHelper {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002767 /// \brief Utility function to check whether or not a sign or zero extension
2768 /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by
2769 /// either using the operands of \p Inst or promoting \p Inst.
2770 /// The type of the extension is defined by \p IsSExt.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002771 /// In other words, check if:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002772 /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002773 /// #1 Promotion applies:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002774 /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...).
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002775 /// #2 Operand reuses:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002776 /// ext opnd1 to ConsideredExtType.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002777 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002778 static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType,
2779 const InstrToOrigTy &PromotedInsts, bool IsSExt);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002780
2781 /// \brief Utility function to determine if \p OpIdx should be promoted when
2782 /// promoting \p Inst.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002783 static bool shouldExtOperand(const Instruction *Inst, int OpIdx) {
Rafael Espindola84921b92015-10-24 23:11:13 +00002784 return !(isa<SelectInst>(Inst) && OpIdx == 0);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002785 }
2786
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002787 /// \brief Utility function to promote the operand of \p Ext when this
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002788 /// operand is a promotable trunc or sext or zext.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002789 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002790 /// \p CreatedInstsCost[out] contains the cost of all instructions
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002791 /// created to promote the operand of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002792 /// Newly added extensions are inserted in \p Exts.
2793 /// Newly added truncates are inserted in \p Truncs.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002794 /// Should never be called directly.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002795 /// \return The promoted value which is used instead of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002796 static Value *promoteOperandForTruncAndAnyExt(
2797 Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002798 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002799 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002800 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002801
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002802 /// \brief Utility function to promote the operand of \p Ext when this
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002803 /// operand is promotable and is not a supported trunc or sext.
2804 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002805 /// \p CreatedInstsCost[out] contains the cost of all the instructions
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002806 /// created to promote the operand of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002807 /// Newly added extensions are inserted in \p Exts.
2808 /// Newly added truncates are inserted in \p Truncs.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002809 /// Should never be called directly.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002810 /// \return The promoted value which is used instead of Ext.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002811 static Value *promoteOperandForOther(Instruction *Ext,
2812 TypePromotionTransaction &TPT,
2813 InstrToOrigTy &PromotedInsts,
2814 unsigned &CreatedInstsCost,
2815 SmallVectorImpl<Instruction *> *Exts,
2816 SmallVectorImpl<Instruction *> *Truncs,
2817 const TargetLowering &TLI, bool IsSExt);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002818
2819 /// \see promoteOperandForOther.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002820 static Value *signExtendOperandForOther(
2821 Instruction *Ext, TypePromotionTransaction &TPT,
2822 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
2823 SmallVectorImpl<Instruction *> *Exts,
2824 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
2825 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
2826 Exts, Truncs, TLI, true);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002827 }
2828
2829 /// \see promoteOperandForOther.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002830 static Value *zeroExtendOperandForOther(
2831 Instruction *Ext, TypePromotionTransaction &TPT,
2832 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
2833 SmallVectorImpl<Instruction *> *Exts,
2834 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
2835 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
2836 Exts, Truncs, TLI, false);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002837 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002838
2839public:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002840 /// Type for the utility function that promotes the operand of Ext.
2841 typedef Value *(*Action)(Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002842 InstrToOrigTy &PromotedInsts,
2843 unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002844 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002845 SmallVectorImpl<Instruction *> *Truncs,
2846 const TargetLowering &TLI);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002847 /// \brief Given a sign/zero extend instruction \p Ext, return the approriate
2848 /// action to promote the operand of \p Ext instead of using Ext.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002849 /// \return NULL if no promotable action is possible with the current
2850 /// sign extension.
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002851 /// \p InsertedInsts keeps track of all the instructions inserted by the
2852 /// other CodeGenPrepare optimizations. This information is important
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002853 /// because we do not want to promote these instructions as CodeGenPrepare
2854 /// will reinsert them later. Thus creating an infinite loop: create/remove.
2855 /// \p PromotedInsts maps the instructions to their type before promotion.
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002856 static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedInsts,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002857 const TargetLowering &TLI,
2858 const InstrToOrigTy &PromotedInsts);
2859};
2860
2861bool TypePromotionHelper::canGetThrough(const Instruction *Inst,
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002862 Type *ConsideredExtType,
2863 const InstrToOrigTy &PromotedInsts,
2864 bool IsSExt) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002865 // The promotion helper does not know how to deal with vector types yet.
2866 // To be able to fix that, we would need to fix the places where we
2867 // statically extend, e.g., constants and such.
2868 if (Inst->getType()->isVectorTy())
2869 return false;
2870
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002871 // We can always get through zext.
2872 if (isa<ZExtInst>(Inst))
2873 return true;
2874
2875 // sext(sext) is ok too.
2876 if (IsSExt && isa<SExtInst>(Inst))
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002877 return true;
2878
2879 // We can get through binary operator, if it is legal. In other words, the
2880 // binary operator must have a nuw or nsw flag.
2881 const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst);
2882 if (BinOp && isa<OverflowingBinaryOperator>(BinOp) &&
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002883 ((!IsSExt && BinOp->hasNoUnsignedWrap()) ||
2884 (IsSExt && BinOp->hasNoSignedWrap())))
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002885 return true;
2886
2887 // Check if we can do the following simplification.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002888 // ext(trunc(opnd)) --> ext(opnd)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002889 if (!isa<TruncInst>(Inst))
2890 return false;
2891
2892 Value *OpndVal = Inst->getOperand(0);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002893 // Check if we can use this operand in the extension.
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00002894 // If the type is larger than the result type of the extension, we cannot.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002895 if (!OpndVal->getType()->isIntegerTy() ||
2896 OpndVal->getType()->getIntegerBitWidth() >
2897 ConsideredExtType->getIntegerBitWidth())
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002898 return false;
2899
2900 // If the operand of the truncate is not an instruction, we will not have
2901 // any information on the dropped bits.
2902 // (Actually we could for constant but it is not worth the extra logic).
2903 Instruction *Opnd = dyn_cast<Instruction>(OpndVal);
2904 if (!Opnd)
2905 return false;
2906
2907 // Check if the source of the type is narrow enough.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002908 // I.e., check that trunc just drops extended bits of the same kind of
2909 // the extension.
2910 // #1 get the type of the operand and check the kind of the extended bits.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002911 const Type *OpndType;
2912 InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd);
Benjamin Kramer4cd5faa2015-07-31 17:00:39 +00002913 if (It != PromotedInsts.end() && It->second.getInt() == IsSExt)
2914 OpndType = It->second.getPointer();
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002915 else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd)))
2916 OpndType = Opnd->getOperand(0)->getType();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002917 else
2918 return false;
2919
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00002920 // #2 check that the truncate just drops extended bits.
Rafael Espindola84921b92015-10-24 23:11:13 +00002921 return Inst->getType()->getIntegerBitWidth() >=
2922 OpndType->getIntegerBitWidth();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002923}
2924
2925TypePromotionHelper::Action TypePromotionHelper::getAction(
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002926 Instruction *Ext, const SetOfInstrs &InsertedInsts,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002927 const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002928 assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
2929 "Unexpected instruction type");
2930 Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0));
2931 Type *ExtTy = Ext->getType();
2932 bool IsSExt = isa<SExtInst>(Ext);
2933 // If the operand of the extension is not an instruction, we cannot
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002934 // get through.
2935 // If it, check we can get through.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002936 if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt))
Craig Topperc0196b12014-04-14 00:51:57 +00002937 return nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002938
2939 // Do not promote if the operand has been added by codegenprepare.
2940 // Otherwise, it means we are undoing an optimization that is likely to be
2941 // redone, thus causing potential infinite loop.
Ahmed Bougachaf3299142015-06-17 20:44:32 +00002942 if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd))
Craig Topperc0196b12014-04-14 00:51:57 +00002943 return nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002944
2945 // SExt or Trunc instructions.
2946 // Return the related handler.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002947 if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) ||
2948 isa<ZExtInst>(ExtOpnd))
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002949 return promoteOperandForTruncAndAnyExt;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002950
2951 // Regular instruction.
2952 // Abort early if we will have to insert non-free instructions.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002953 if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType()))
Craig Topperc0196b12014-04-14 00:51:57 +00002954 return nullptr;
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002955 return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002956}
2957
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002958Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt(
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002959 llvm::Instruction *SExt, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002960 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002961 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002962 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002963 // By construction, the operand of SExt is an instruction. Otherwise we cannot
2964 // get through it and this method should not be called.
2965 Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0));
Quentin Colombetac55b152014-09-16 22:36:07 +00002966 Value *ExtVal = SExt;
Quentin Colombet1b274f92015-03-10 21:48:15 +00002967 bool HasMergedNonFreeExt = false;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002968 if (isa<ZExtInst>(SExtOpnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002969 // Replace s|zext(zext(opnd))
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002970 // => zext(opnd).
Quentin Colombet1b274f92015-03-10 21:48:15 +00002971 HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd);
Quentin Colombetac55b152014-09-16 22:36:07 +00002972 Value *ZExt =
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002973 TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType());
2974 TPT.replaceAllUsesWith(SExt, ZExt);
2975 TPT.eraseInstruction(SExt);
Quentin Colombetac55b152014-09-16 22:36:07 +00002976 ExtVal = ZExt;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002977 } else {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002978 // Replace z|sext(trunc(opnd)) or sext(sext(opnd))
2979 // => z|sext(opnd).
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002980 TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0));
2981 }
Quentin Colombet1b274f92015-03-10 21:48:15 +00002982 CreatedInstsCost = 0;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002983
2984 // Remove dead code.
2985 if (SExtOpnd->use_empty())
2986 TPT.eraseInstruction(SExtOpnd);
2987
Quentin Colombet9dcb7242014-09-15 18:26:58 +00002988 // Check if the extension is still needed.
Quentin Colombetac55b152014-09-16 22:36:07 +00002989 Instruction *ExtInst = dyn_cast<Instruction>(ExtVal);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002990 if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) {
Quentin Colombet1b274f92015-03-10 21:48:15 +00002991 if (ExtInst) {
2992 if (Exts)
2993 Exts->push_back(ExtInst);
2994 CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt;
2995 }
Quentin Colombetac55b152014-09-16 22:36:07 +00002996 return ExtVal;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002997 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002998
Quentin Colombet9dcb7242014-09-15 18:26:58 +00002999 // At this point we have: ext ty opnd to ty.
3000 // Reassign the uses of ExtInst to the opnd and remove ExtInst.
3001 Value *NextVal = ExtInst->getOperand(0);
3002 TPT.eraseInstruction(ExtInst, NextVal);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003003 return NextVal;
3004}
3005
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003006Value *TypePromotionHelper::promoteOperandForOther(
3007 Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00003008 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003009 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00003010 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI,
3011 bool IsSExt) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003012 // By construction, the operand of Ext is an instruction. Otherwise we cannot
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003013 // get through it and this method should not be called.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003014 Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0));
Quentin Colombet1b274f92015-03-10 21:48:15 +00003015 CreatedInstsCost = 0;
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003016 if (!ExtOpnd->hasOneUse()) {
3017 // ExtOpnd will be promoted.
3018 // All its uses, but Ext, will need to use a truncated value of the
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003019 // promoted version.
3020 // Create the truncate now.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003021 Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType());
Quentin Colombetac55b152014-09-16 22:36:07 +00003022 if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) {
3023 ITrunc->removeFromParent();
3024 // Insert it just after the definition.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003025 ITrunc->insertAfter(ExtOpnd);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003026 if (Truncs)
3027 Truncs->push_back(ITrunc);
Quentin Colombetac55b152014-09-16 22:36:07 +00003028 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003029
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003030 TPT.replaceAllUsesWith(ExtOpnd, Trunc);
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003031 // Restore the operand of Ext (which has been replaced by the previous call
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003032 // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003033 TPT.setOperand(Ext, 0, ExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003034 }
3035
3036 // Get through the Instruction:
3037 // 1. Update its type.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003038 // 2. Replace the uses of Ext by Inst.
3039 // 3. Extend each operand that needs to be extended.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003040
3041 // Remember the original type of the instruction before promotion.
3042 // This is useful to know that the high bits are sign extended bits.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003043 PromotedInsts.insert(std::pair<Instruction *, TypeIsSExt>(
3044 ExtOpnd, TypeIsSExt(ExtOpnd->getType(), IsSExt)));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003045 // Step #1.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003046 TPT.mutateType(ExtOpnd, Ext->getType());
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003047 // Step #2.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003048 TPT.replaceAllUsesWith(Ext, ExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003049 // Step #3.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003050 Instruction *ExtForOpnd = Ext;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003051
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003052 DEBUG(dbgs() << "Propagate Ext to operands\n");
3053 for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003054 ++OpIdx) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003055 DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n');
3056 if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() ||
3057 !shouldExtOperand(ExtOpnd, OpIdx)) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003058 DEBUG(dbgs() << "No need to propagate\n");
3059 continue;
3060 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003061 // Check if we can statically extend the operand.
3062 Value *Opnd = ExtOpnd->getOperand(OpIdx);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003063 if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003064 DEBUG(dbgs() << "Statically extend\n");
3065 unsigned BitWidth = Ext->getType()->getIntegerBitWidth();
3066 APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth)
3067 : Cst->getValue().zext(BitWidth);
3068 TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003069 continue;
3070 }
3071 // UndefValue are typed, so we have to statically sign extend them.
3072 if (isa<UndefValue>(Opnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003073 DEBUG(dbgs() << "Statically extend\n");
3074 TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType()));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003075 continue;
3076 }
3077
3078 // Otherwise we have to explicity sign extend the operand.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003079 // Check if Ext was reused to extend an operand.
3080 if (!ExtForOpnd) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003081 // If yes, create a new one.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003082 DEBUG(dbgs() << "More operands to ext\n");
Quentin Colombet84f89cc2014-12-22 18:11:52 +00003083 Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->getType())
3084 : TPT.createZExt(Ext, Opnd, Ext->getType());
3085 if (!isa<Instruction>(ValForExtOpnd)) {
3086 TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd);
3087 continue;
3088 }
3089 ExtForOpnd = cast<Instruction>(ValForExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003090 }
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003091 if (Exts)
3092 Exts->push_back(ExtForOpnd);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003093 TPT.setOperand(ExtForOpnd, 0, Opnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003094
3095 // Move the sign extension before the insertion point.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003096 TPT.moveBefore(ExtForOpnd, ExtOpnd);
3097 TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd);
Quentin Colombet1b274f92015-03-10 21:48:15 +00003098 CreatedInstsCost += !TLI.isExtFree(ExtForOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003099 // If more sext are required, new instructions will have to be created.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003100 ExtForOpnd = nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003101 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003102 if (ExtForOpnd == Ext) {
3103 DEBUG(dbgs() << "Extension is useless now\n");
3104 TPT.eraseInstruction(Ext);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003105 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003106 return ExtOpnd;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003107}
3108
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003109/// Check whether or not promoting an instruction to a wider type is profitable.
Quentin Colombet1b274f92015-03-10 21:48:15 +00003110/// \p NewCost gives the cost of extension instructions created by the
3111/// promotion.
3112/// \p OldCost gives the cost of extension instructions before the promotion
3113/// plus the number of instructions that have been
3114/// matched in the addressing mode the promotion.
Quentin Colombet867c5502014-02-14 22:23:22 +00003115/// \p PromotedOperand is the value that has been promoted.
3116/// \return True if the promotion is profitable, false otherwise.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003117bool AddressingModeMatcher::isPromotionProfitable(
Quentin Colombet1b274f92015-03-10 21:48:15 +00003118 unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const {
3119 DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n');
3120 // The cost of the new extensions is greater than the cost of the
3121 // old extension plus what we folded.
Quentin Colombet867c5502014-02-14 22:23:22 +00003122 // This is not profitable.
Quentin Colombet1b274f92015-03-10 21:48:15 +00003123 if (NewCost > OldCost)
Quentin Colombet867c5502014-02-14 22:23:22 +00003124 return false;
Quentin Colombet1b274f92015-03-10 21:48:15 +00003125 if (NewCost < OldCost)
Quentin Colombet867c5502014-02-14 22:23:22 +00003126 return true;
3127 // The promotion is neutral but it may help folding the sign extension in
3128 // loads for instance.
3129 // Check that we did not create an illegal instruction.
Mehdi Amini44ede332015-07-09 02:09:04 +00003130 return isPromotedInstructionLegal(TLI, DL, PromotedOperand);
Quentin Colombet867c5502014-02-14 22:23:22 +00003131}
3132
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003133/// Given an instruction or constant expr, see if we can fold the operation
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003134/// into the addressing mode. If so, update the addressing mode and return
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003135/// true, otherwise return false without modifying AddrMode.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003136/// If \p MovedAway is not NULL, it contains the information of whether or
3137/// not AddrInst has to be folded into the addressing mode on success.
3138/// If \p MovedAway == true, \p AddrInst will not be part of the addressing
3139/// because it has been moved away.
3140/// Thus AddrInst must not be added in the matched instructions.
3141/// This state can happen when AddrInst is a sext, since it may be moved away.
3142/// Therefore, AddrInst may not be valid when MovedAway is true and it must
3143/// not be referenced anymore.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003144bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003145 unsigned Depth,
3146 bool *MovedAway) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003147 // Avoid exponential behavior on extremely deep expression trees.
3148 if (Depth >= 5) return false;
Stephen Lin837bba12013-07-15 17:55:02 +00003149
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003150 // By default, all matched instructions stay in place.
3151 if (MovedAway)
3152 *MovedAway = false;
3153
Chandler Carruthc8925912013-01-05 02:09:22 +00003154 switch (Opcode) {
3155 case Instruction::PtrToInt:
3156 // PtrToInt is always a noop, as we know that the int type is pointer sized.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003157 return matchAddr(AddrInst->getOperand(0), Depth);
Mehdi Amini44ede332015-07-09 02:09:04 +00003158 case Instruction::IntToPtr: {
3159 auto AS = AddrInst->getType()->getPointerAddressSpace();
3160 auto PtrTy = MVT::getIntegerVT(DL.getPointerSizeInBits(AS));
Chandler Carruthc8925912013-01-05 02:09:22 +00003161 // This inttoptr is a no-op if the integer type is pointer sized.
Mehdi Amini44ede332015-07-09 02:09:04 +00003162 if (TLI.getValueType(DL, AddrInst->getOperand(0)->getType()) == PtrTy)
Sanjay Patelfc580a62015-09-21 23:03:16 +00003163 return matchAddr(AddrInst->getOperand(0), Depth);
Chandler Carruthc8925912013-01-05 02:09:22 +00003164 return false;
Mehdi Amini44ede332015-07-09 02:09:04 +00003165 }
Chandler Carruthc8925912013-01-05 02:09:22 +00003166 case Instruction::BitCast:
3167 // BitCast is always a noop, and we can handle it as long as it is
3168 // int->int or pointer->pointer (we don't want int<->fp or something).
3169 if ((AddrInst->getOperand(0)->getType()->isPointerTy() ||
3170 AddrInst->getOperand(0)->getType()->isIntegerTy()) &&
3171 // Don't touch identity bitcasts. These were probably put here by LSR,
3172 // and we don't want to mess around with them. Assume it knows what it
3173 // is doing.
3174 AddrInst->getOperand(0)->getType() != AddrInst->getType())
Sanjay Patelfc580a62015-09-21 23:03:16 +00003175 return matchAddr(AddrInst->getOperand(0), Depth);
Chandler Carruthc8925912013-01-05 02:09:22 +00003176 return false;
Matt Arsenaultf05b0232015-05-26 16:59:43 +00003177 case Instruction::AddrSpaceCast: {
3178 unsigned SrcAS
3179 = AddrInst->getOperand(0)->getType()->getPointerAddressSpace();
3180 unsigned DestAS = AddrInst->getType()->getPointerAddressSpace();
3181 if (TLI.isNoopAddrSpaceCast(SrcAS, DestAS))
Sanjay Patelfc580a62015-09-21 23:03:16 +00003182 return matchAddr(AddrInst->getOperand(0), Depth);
Matt Arsenaultf05b0232015-05-26 16:59:43 +00003183 return false;
3184 }
Chandler Carruthc8925912013-01-05 02:09:22 +00003185 case Instruction::Add: {
3186 // Check to see if we can merge in the RHS then the LHS. If so, we win.
3187 ExtAddrMode BackupAddrMode = AddrMode;
3188 unsigned OldSize = AddrModeInsts.size();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003189 // Start a transaction at this point.
3190 // The LHS may match but not the RHS.
3191 // Therefore, we need a higher level restoration point to undo partially
3192 // matched operation.
3193 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3194 TPT.getRestorationPoint();
3195
Sanjay Patelfc580a62015-09-21 23:03:16 +00003196 if (matchAddr(AddrInst->getOperand(1), Depth+1) &&
3197 matchAddr(AddrInst->getOperand(0), Depth+1))
Chandler Carruthc8925912013-01-05 02:09:22 +00003198 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003199
Chandler Carruthc8925912013-01-05 02:09:22 +00003200 // Restore the old addr mode info.
3201 AddrMode = BackupAddrMode;
3202 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003203 TPT.rollback(LastKnownGood);
Stephen Lin837bba12013-07-15 17:55:02 +00003204
Chandler Carruthc8925912013-01-05 02:09:22 +00003205 // Otherwise this was over-aggressive. Try merging in the LHS then the RHS.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003206 if (matchAddr(AddrInst->getOperand(0), Depth+1) &&
3207 matchAddr(AddrInst->getOperand(1), Depth+1))
Chandler Carruthc8925912013-01-05 02:09:22 +00003208 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003209
Chandler Carruthc8925912013-01-05 02:09:22 +00003210 // Otherwise we definitely can't merge the ADD in.
3211 AddrMode = BackupAddrMode;
3212 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003213 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00003214 break;
3215 }
3216 //case Instruction::Or:
3217 // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
3218 //break;
3219 case Instruction::Mul:
3220 case Instruction::Shl: {
3221 // Can only handle X*C and X << C.
3222 ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
Sanjay Pateld3bbfa12014-07-16 22:40:28 +00003223 if (!RHS)
3224 return false;
Chandler Carruthc8925912013-01-05 02:09:22 +00003225 int64_t Scale = RHS->getSExtValue();
3226 if (Opcode == Instruction::Shl)
3227 Scale = 1LL << Scale;
Stephen Lin837bba12013-07-15 17:55:02 +00003228
Sanjay Patelfc580a62015-09-21 23:03:16 +00003229 return matchScaledValue(AddrInst->getOperand(0), Scale, Depth);
Chandler Carruthc8925912013-01-05 02:09:22 +00003230 }
3231 case Instruction::GetElementPtr: {
3232 // Scan the GEP. We check it if it contains constant offsets and at most
3233 // one variable offset.
3234 int VariableOperand = -1;
3235 unsigned VariableScale = 0;
Stephen Lin837bba12013-07-15 17:55:02 +00003236
Chandler Carruthc8925912013-01-05 02:09:22 +00003237 int64_t ConstantOffset = 0;
Chandler Carruthc8925912013-01-05 02:09:22 +00003238 gep_type_iterator GTI = gep_type_begin(AddrInst);
3239 for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
3240 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Mehdi Amini4fe37982015-07-07 18:45:17 +00003241 const StructLayout *SL = DL.getStructLayout(STy);
Chandler Carruthc8925912013-01-05 02:09:22 +00003242 unsigned Idx =
3243 cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
3244 ConstantOffset += SL->getElementOffset(Idx);
3245 } else {
Mehdi Amini4fe37982015-07-07 18:45:17 +00003246 uint64_t TypeSize = DL.getTypeAllocSize(GTI.getIndexedType());
Chandler Carruthc8925912013-01-05 02:09:22 +00003247 if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
3248 ConstantOffset += CI->getSExtValue()*TypeSize;
3249 } else if (TypeSize) { // Scales of zero don't do anything.
3250 // We only allow one variable index at the moment.
3251 if (VariableOperand != -1)
3252 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00003253
Chandler Carruthc8925912013-01-05 02:09:22 +00003254 // Remember the variable index.
3255 VariableOperand = i;
3256 VariableScale = TypeSize;
3257 }
3258 }
3259 }
Stephen Lin837bba12013-07-15 17:55:02 +00003260
Chandler Carruthc8925912013-01-05 02:09:22 +00003261 // A common case is for the GEP to only do a constant offset. In this case,
3262 // just add it to the disp field and check validity.
3263 if (VariableOperand == -1) {
3264 AddrMode.BaseOffs += ConstantOffset;
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003265 if (ConstantOffset == 0 ||
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003266 TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003267 // Check to see if we can fold the base pointer in too.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003268 if (matchAddr(AddrInst->getOperand(0), Depth+1))
Chandler Carruthc8925912013-01-05 02:09:22 +00003269 return true;
3270 }
3271 AddrMode.BaseOffs -= ConstantOffset;
3272 return false;
3273 }
3274
3275 // Save the valid addressing mode in case we can't match.
3276 ExtAddrMode BackupAddrMode = AddrMode;
3277 unsigned OldSize = AddrModeInsts.size();
3278
3279 // See if the scale and offset amount is valid for this target.
3280 AddrMode.BaseOffs += ConstantOffset;
3281
3282 // Match the base operand of the GEP.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003283 if (!matchAddr(AddrInst->getOperand(0), Depth+1)) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003284 // If it couldn't be matched, just stuff the value in a register.
3285 if (AddrMode.HasBaseReg) {
3286 AddrMode = BackupAddrMode;
3287 AddrModeInsts.resize(OldSize);
3288 return false;
3289 }
3290 AddrMode.HasBaseReg = true;
3291 AddrMode.BaseReg = AddrInst->getOperand(0);
3292 }
3293
3294 // Match the remaining variable portion of the GEP.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003295 if (!matchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
Chandler Carruthc8925912013-01-05 02:09:22 +00003296 Depth)) {
3297 // If it couldn't be matched, try stuffing the base into a register
3298 // instead of matching it, and retrying the match of the scale.
3299 AddrMode = BackupAddrMode;
3300 AddrModeInsts.resize(OldSize);
3301 if (AddrMode.HasBaseReg)
3302 return false;
3303 AddrMode.HasBaseReg = true;
3304 AddrMode.BaseReg = AddrInst->getOperand(0);
3305 AddrMode.BaseOffs += ConstantOffset;
Sanjay Patelfc580a62015-09-21 23:03:16 +00003306 if (!matchScaledValue(AddrInst->getOperand(VariableOperand),
Chandler Carruthc8925912013-01-05 02:09:22 +00003307 VariableScale, Depth)) {
3308 // If even that didn't work, bail.
3309 AddrMode = BackupAddrMode;
3310 AddrModeInsts.resize(OldSize);
3311 return false;
3312 }
3313 }
3314
3315 return true;
3316 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003317 case Instruction::SExt:
3318 case Instruction::ZExt: {
3319 Instruction *Ext = dyn_cast<Instruction>(AddrInst);
3320 if (!Ext)
Sanjay Pateld3bbfa12014-07-16 22:40:28 +00003321 return false;
Sanjay Patelab60d042014-07-16 21:08:10 +00003322
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003323 // Try to move this ext out of the way of the addressing mode.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003324 // Ask for a method for doing so.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003325 TypePromotionHelper::Action TPH =
Ahmed Bougachaf3299142015-06-17 20:44:32 +00003326 TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003327 if (!TPH)
3328 return false;
3329
3330 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3331 TPT.getRestorationPoint();
Quentin Colombet1b274f92015-03-10 21:48:15 +00003332 unsigned CreatedInstsCost = 0;
3333 unsigned ExtCost = !TLI.isExtFree(Ext);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003334 Value *PromotedOperand =
Quentin Colombet1b274f92015-03-10 21:48:15 +00003335 TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003336 // SExt has been moved away.
3337 // Thus either it will be rematched later in the recursive calls or it is
3338 // gone. Anyway, we must not fold it into the addressing mode at this point.
3339 // E.g.,
3340 // op = add opnd, 1
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003341 // idx = ext op
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003342 // addr = gep base, idx
3343 // is now:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00003344 // promotedOpnd = ext opnd <- no match here
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003345 // op = promoted_add promotedOpnd, 1 <- match (later in recursive calls)
3346 // addr = gep base, op <- match
3347 if (MovedAway)
3348 *MovedAway = true;
3349
3350 assert(PromotedOperand &&
3351 "TypePromotionHelper should have filtered out those cases");
3352
3353 ExtAddrMode BackupAddrMode = AddrMode;
3354 unsigned OldSize = AddrModeInsts.size();
3355
Sanjay Patelfc580a62015-09-21 23:03:16 +00003356 if (!matchAddr(PromotedOperand, Depth) ||
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003357 // The total of the new cost is equal to the cost of the created
Quentin Colombet1b274f92015-03-10 21:48:15 +00003358 // instructions.
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003359 // The total of the old cost is equal to the cost of the extension plus
Quentin Colombet1b274f92015-03-10 21:48:15 +00003360 // what we have saved in the addressing mode.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003361 !isPromotionProfitable(CreatedInstsCost,
Quentin Colombet1b274f92015-03-10 21:48:15 +00003362 ExtCost + (AddrModeInsts.size() - OldSize),
Quentin Colombet867c5502014-02-14 22:23:22 +00003363 PromotedOperand)) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003364 AddrMode = BackupAddrMode;
3365 AddrModeInsts.resize(OldSize);
3366 DEBUG(dbgs() << "Sign extension does not pay off: rollback\n");
3367 TPT.rollback(LastKnownGood);
3368 return false;
3369 }
3370 return true;
3371 }
Chandler Carruthc8925912013-01-05 02:09:22 +00003372 }
3373 return false;
3374}
3375
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003376/// If we can, try to add the value of 'Addr' into the current addressing mode.
3377/// If Addr can't be added to AddrMode this returns false and leaves AddrMode
3378/// unmodified. This assumes that Addr is either a pointer type or intptr_t
3379/// for the target.
Chandler Carruthc8925912013-01-05 02:09:22 +00003380///
Sanjay Patelfc580a62015-09-21 23:03:16 +00003381bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003382 // Start a transaction at this point that we will rollback if the matching
3383 // fails.
3384 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3385 TPT.getRestorationPoint();
Chandler Carruthc8925912013-01-05 02:09:22 +00003386 if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
3387 // Fold in immediates if legal for the target.
3388 AddrMode.BaseOffs += CI->getSExtValue();
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003389 if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
Chandler Carruthc8925912013-01-05 02:09:22 +00003390 return true;
3391 AddrMode.BaseOffs -= CI->getSExtValue();
3392 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
3393 // If this is a global variable, try to fold it into the addressing mode.
Craig Topperc0196b12014-04-14 00:51:57 +00003394 if (!AddrMode.BaseGV) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003395 AddrMode.BaseGV = GV;
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003396 if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
Chandler Carruthc8925912013-01-05 02:09:22 +00003397 return true;
Craig Topperc0196b12014-04-14 00:51:57 +00003398 AddrMode.BaseGV = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003399 }
3400 } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
3401 ExtAddrMode BackupAddrMode = AddrMode;
3402 unsigned OldSize = AddrModeInsts.size();
3403
3404 // Check to see if it is possible to fold this operation.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003405 bool MovedAway = false;
Sanjay Patelfc580a62015-09-21 23:03:16 +00003406 if (matchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) {
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003407 // This instruction may have been moved away. If so, there is nothing
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003408 // to check here.
3409 if (MovedAway)
3410 return true;
Chandler Carruthc8925912013-01-05 02:09:22 +00003411 // Okay, it's possible to fold this. Check to see if it is actually
3412 // *profitable* to do so. We use a simple cost model to avoid increasing
3413 // register pressure too much.
3414 if (I->hasOneUse() ||
Sanjay Patelfc580a62015-09-21 23:03:16 +00003415 isProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003416 AddrModeInsts.push_back(I);
3417 return true;
3418 }
Stephen Lin837bba12013-07-15 17:55:02 +00003419
Chandler Carruthc8925912013-01-05 02:09:22 +00003420 // It isn't profitable to do this, roll back.
3421 //cerr << "NOT FOLDING: " << *I;
3422 AddrMode = BackupAddrMode;
3423 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003424 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00003425 }
3426 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
Sanjay Patelfc580a62015-09-21 23:03:16 +00003427 if (matchOperationAddr(CE, CE->getOpcode(), Depth))
Chandler Carruthc8925912013-01-05 02:09:22 +00003428 return true;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003429 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00003430 } else if (isa<ConstantPointerNull>(Addr)) {
3431 // Null pointer gets folded without affecting the addressing mode.
3432 return true;
3433 }
3434
3435 // Worse case, the target should support [reg] addressing modes. :)
3436 if (!AddrMode.HasBaseReg) {
3437 AddrMode.HasBaseReg = true;
3438 AddrMode.BaseReg = Addr;
3439 // Still check for legality in case the target supports [imm] but not [i+r].
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003440 if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
Chandler Carruthc8925912013-01-05 02:09:22 +00003441 return true;
3442 AddrMode.HasBaseReg = false;
Craig Topperc0196b12014-04-14 00:51:57 +00003443 AddrMode.BaseReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003444 }
3445
3446 // If the base register is already taken, see if we can do [r+r].
3447 if (AddrMode.Scale == 0) {
3448 AddrMode.Scale = 1;
3449 AddrMode.ScaledReg = Addr;
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003450 if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
Chandler Carruthc8925912013-01-05 02:09:22 +00003451 return true;
3452 AddrMode.Scale = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00003453 AddrMode.ScaledReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003454 }
3455 // Couldn't match.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003456 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00003457 return false;
3458}
3459
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003460/// Check to see if all uses of OpVal by the specified inline asm call are due
3461/// to memory operands. If so, return true, otherwise return false.
Chandler Carruthc8925912013-01-05 02:09:22 +00003462static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
Eric Christopher11e4df72015-02-26 22:38:43 +00003463 const TargetMachine &TM) {
3464 const Function *F = CI->getParent()->getParent();
3465 const TargetLowering *TLI = TM.getSubtargetImpl(*F)->getTargetLowering();
3466 const TargetRegisterInfo *TRI = TM.getSubtargetImpl(*F)->getRegisterInfo();
Eric Christopherd75c00c2015-02-26 22:38:34 +00003467 TargetLowering::AsmOperandInfoVector TargetConstraints =
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00003468 TLI->ParseConstraints(F->getParent()->getDataLayout(), TRI,
3469 ImmutableCallSite(CI));
Chandler Carruthc8925912013-01-05 02:09:22 +00003470 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
3471 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
Stephen Lin837bba12013-07-15 17:55:02 +00003472
Chandler Carruthc8925912013-01-05 02:09:22 +00003473 // Compute the constraint code and ConstraintType to use.
Eric Christopher11e4df72015-02-26 22:38:43 +00003474 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chandler Carruthc8925912013-01-05 02:09:22 +00003475
3476 // If this asm operand is our Value*, and if it isn't an indirect memory
3477 // operand, we can't fold it!
3478 if (OpInfo.CallOperandVal == OpVal &&
3479 (OpInfo.ConstraintType != TargetLowering::C_Memory ||
3480 !OpInfo.isIndirect))
3481 return false;
3482 }
3483
3484 return true;
3485}
3486
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003487/// Recursively walk all the uses of I until we find a memory use.
3488/// If we find an obviously non-foldable instruction, return true.
Chandler Carruthc8925912013-01-05 02:09:22 +00003489/// Add the ultimately found memory instructions to MemoryUses.
Eric Christopher11e4df72015-02-26 22:38:43 +00003490static bool FindAllMemoryUses(
3491 Instruction *I,
3492 SmallVectorImpl<std::pair<Instruction *, unsigned>> &MemoryUses,
3493 SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetMachine &TM) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003494 // If we already considered this instruction, we're done.
David Blaikie70573dc2014-11-19 07:49:26 +00003495 if (!ConsideredInsts.insert(I).second)
Chandler Carruthc8925912013-01-05 02:09:22 +00003496 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00003497
Chandler Carruthc8925912013-01-05 02:09:22 +00003498 // If this is an obviously unfoldable instruction, bail out.
3499 if (!MightBeFoldableInst(I))
3500 return true;
3501
Philip Reamesac115ed2016-03-09 23:13:12 +00003502 const bool OptSize = I->getFunction()->optForSize();
3503
Chandler Carruthc8925912013-01-05 02:09:22 +00003504 // Loop over all the uses, recursively processing them.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003505 for (Use &U : I->uses()) {
3506 Instruction *UserI = cast<Instruction>(U.getUser());
Chandler Carruthc8925912013-01-05 02:09:22 +00003507
Chandler Carruthcdf47882014-03-09 03:16:01 +00003508 if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) {
3509 MemoryUses.push_back(std::make_pair(LI, U.getOperandNo()));
Chandler Carruthc8925912013-01-05 02:09:22 +00003510 continue;
3511 }
Stephen Lin837bba12013-07-15 17:55:02 +00003512
Chandler Carruthcdf47882014-03-09 03:16:01 +00003513 if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
3514 unsigned opNo = U.getOperandNo();
Chandler Carruthc8925912013-01-05 02:09:22 +00003515 if (opNo == 0) return true; // Storing addr, not into addr.
3516 MemoryUses.push_back(std::make_pair(SI, opNo));
3517 continue;
3518 }
Stephen Lin837bba12013-07-15 17:55:02 +00003519
Chandler Carruthcdf47882014-03-09 03:16:01 +00003520 if (CallInst *CI = dyn_cast<CallInst>(UserI)) {
Philip Reamesac115ed2016-03-09 23:13:12 +00003521 // If this is a cold call, we can sink the addressing calculation into
3522 // the cold path. See optimizeCallInst
3523 if (!OptSize && CI->hasFnAttr(Attribute::Cold))
3524 continue;
Junmo Park6098cbb2016-03-11 07:05:32 +00003525
Chandler Carruthc8925912013-01-05 02:09:22 +00003526 InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue());
3527 if (!IA) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003528
Chandler Carruthc8925912013-01-05 02:09:22 +00003529 // If this is a memory operand, we're cool, otherwise bail out.
Eric Christopher11e4df72015-02-26 22:38:43 +00003530 if (!IsOperandAMemoryOperand(CI, IA, I, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003531 return true;
3532 continue;
3533 }
Stephen Lin837bba12013-07-15 17:55:02 +00003534
Eric Christopher11e4df72015-02-26 22:38:43 +00003535 if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003536 return true;
3537 }
3538
3539 return false;
3540}
3541
Sanjay Patel9fbe22b2015-10-09 18:01:03 +00003542/// Return true if Val is already known to be live at the use site that we're
3543/// folding it into. If so, there is no cost to include it in the addressing
3544/// mode. KnownLive1 and KnownLive2 are two values that we know are live at the
3545/// instruction already.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003546bool AddressingModeMatcher::valueAlreadyLiveAtInst(Value *Val,Value *KnownLive1,
Chandler Carruthc8925912013-01-05 02:09:22 +00003547 Value *KnownLive2) {
3548 // If Val is either of the known-live values, we know it is live!
Craig Topperc0196b12014-04-14 00:51:57 +00003549 if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2)
Chandler Carruthc8925912013-01-05 02:09:22 +00003550 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003551
Chandler Carruthc8925912013-01-05 02:09:22 +00003552 // All values other than instructions and arguments (e.g. constants) are live.
3553 if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003554
Chandler Carruthc8925912013-01-05 02:09:22 +00003555 // If Val is a constant sized alloca in the entry block, it is live, this is
3556 // true because it is just a reference to the stack/frame pointer, which is
3557 // live for the whole function.
3558 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
3559 if (AI->isStaticAlloca())
3560 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003561
Chandler Carruthc8925912013-01-05 02:09:22 +00003562 // Check to see if this value is already used in the memory instruction's
3563 // block. If so, it's already live into the block at the very least, so we
3564 // can reasonably fold it.
3565 return Val->isUsedInBasicBlock(MemoryInst->getParent());
3566}
3567
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003568/// It is possible for the addressing mode of the machine to fold the specified
3569/// instruction into a load or store that ultimately uses it.
3570/// However, the specified instruction has multiple uses.
3571/// Given this, it may actually increase register pressure to fold it
3572/// into the load. For example, consider this code:
Chandler Carruthc8925912013-01-05 02:09:22 +00003573///
3574/// X = ...
3575/// Y = X+1
3576/// use(Y) -> nonload/store
3577/// Z = Y+1
3578/// load Z
3579///
3580/// In this case, Y has multiple uses, and can be folded into the load of Z
3581/// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to
3582/// be live at the use(Y) line. If we don't fold Y into load Z, we use one
3583/// fewer register. Since Y can't be folded into "use(Y)" we don't increase the
3584/// number of computations either.
3585///
3586/// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If
3587/// X was live across 'load Z' for other reasons, we actually *would* want to
3588/// fold the addressing mode in the Z case. This would make Y die earlier.
3589bool AddressingModeMatcher::
Sanjay Patelfc580a62015-09-21 23:03:16 +00003590isProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore,
Chandler Carruthc8925912013-01-05 02:09:22 +00003591 ExtAddrMode &AMAfter) {
3592 if (IgnoreProfitability) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003593
Chandler Carruthc8925912013-01-05 02:09:22 +00003594 // AMBefore is the addressing mode before this instruction was folded into it,
3595 // and AMAfter is the addressing mode after the instruction was folded. Get
3596 // the set of registers referenced by AMAfter and subtract out those
3597 // referenced by AMBefore: this is the set of values which folding in this
3598 // address extends the lifetime of.
3599 //
3600 // Note that there are only two potential values being referenced here,
3601 // BaseReg and ScaleReg (global addresses are always available, as are any
3602 // folded immediates).
3603 Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
Stephen Lin837bba12013-07-15 17:55:02 +00003604
Chandler Carruthc8925912013-01-05 02:09:22 +00003605 // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
3606 // lifetime wasn't extended by adding this instruction.
Sanjay Patelfc580a62015-09-21 23:03:16 +00003607 if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
Craig Topperc0196b12014-04-14 00:51:57 +00003608 BaseReg = nullptr;
Sanjay Patelfc580a62015-09-21 23:03:16 +00003609 if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
Craig Topperc0196b12014-04-14 00:51:57 +00003610 ScaledReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003611
3612 // If folding this instruction (and it's subexprs) didn't extend any live
3613 // ranges, we're ok with it.
Craig Topperc0196b12014-04-14 00:51:57 +00003614 if (!BaseReg && !ScaledReg)
Chandler Carruthc8925912013-01-05 02:09:22 +00003615 return true;
3616
Philip Reamesac115ed2016-03-09 23:13:12 +00003617 // If all uses of this instruction can have the address mode sunk into them,
3618 // we can remove the addressing mode and effectively trade one live register
3619 // for another (at worst.) In this context, folding an addressing mode into
Junmo Park6098cbb2016-03-11 07:05:32 +00003620 // the use is just a particularly nice way of sinking it.
Chandler Carruthc8925912013-01-05 02:09:22 +00003621 SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses;
3622 SmallPtrSet<Instruction*, 16> ConsideredInsts;
Eric Christopher11e4df72015-02-26 22:38:43 +00003623 if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003624 return false; // Has a non-memory, non-foldable use!
Stephen Lin837bba12013-07-15 17:55:02 +00003625
Chandler Carruthc8925912013-01-05 02:09:22 +00003626 // Now that we know that all uses of this instruction are part of a chain of
3627 // computation involving only operations that could theoretically be folded
Philip Reamesac115ed2016-03-09 23:13:12 +00003628 // into a memory use, loop over each of these memory operation uses and see
3629 // if they could *actually* fold the instruction. The assumption is that
3630 // addressing modes are cheap and that duplicating the computation involved
3631 // many times is worthwhile, even on a fastpath. For sinking candidates
3632 // (i.e. cold call sites), this serves as a way to prevent excessive code
3633 // growth since most architectures have some reasonable small and fast way to
3634 // compute an effective address. (i.e LEA on x86)
Chandler Carruthc8925912013-01-05 02:09:22 +00003635 SmallVector<Instruction*, 32> MatchedAddrModeInsts;
3636 for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) {
3637 Instruction *User = MemoryUses[i].first;
3638 unsigned OpNo = MemoryUses[i].second;
Stephen Lin837bba12013-07-15 17:55:02 +00003639
Chandler Carruthc8925912013-01-05 02:09:22 +00003640 // Get the access type of this use. If the use isn't a pointer, we don't
3641 // know what it accesses.
3642 Value *Address = User->getOperand(OpNo);
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003643 PointerType *AddrTy = dyn_cast<PointerType>(Address->getType());
3644 if (!AddrTy)
Chandler Carruthc8925912013-01-05 02:09:22 +00003645 return false;
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003646 Type *AddressAccessTy = AddrTy->getElementType();
3647 unsigned AS = AddrTy->getAddressSpace();
Stephen Lin837bba12013-07-15 17:55:02 +00003648
Chandler Carruthc8925912013-01-05 02:09:22 +00003649 // Do a match against the root of this address, ignoring profitability. This
3650 // will tell us if the addressing mode for the memory operation will
3651 // *actually* cover the shared instruction.
3652 ExtAddrMode Result;
Quentin Colombet5a69dda2014-02-11 01:59:02 +00003653 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3654 TPT.getRestorationPoint();
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003655 AddressingModeMatcher Matcher(MatchedAddrModeInsts, TM, AddressAccessTy, AS,
Ahmed Bougachaf3299142015-06-17 20:44:32 +00003656 MemoryInst, Result, InsertedInsts,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003657 PromotedInsts, TPT);
Chandler Carruthc8925912013-01-05 02:09:22 +00003658 Matcher.IgnoreProfitability = true;
Sanjay Patelfc580a62015-09-21 23:03:16 +00003659 bool Success = Matcher.matchAddr(Address, 0);
Chandler Carruthc8925912013-01-05 02:09:22 +00003660 (void)Success; assert(Success && "Couldn't select *anything*?");
3661
Quentin Colombet5a69dda2014-02-11 01:59:02 +00003662 // The match was to check the profitability, the changes made are not
3663 // part of the original matcher. Therefore, they should be dropped
3664 // otherwise the original matcher will not present the right state.
3665 TPT.rollback(LastKnownGood);
3666
Chandler Carruthc8925912013-01-05 02:09:22 +00003667 // If the match didn't cover I, then it won't be shared by it.
3668 if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(),
3669 I) == MatchedAddrModeInsts.end())
3670 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00003671
Chandler Carruthc8925912013-01-05 02:09:22 +00003672 MatchedAddrModeInsts.clear();
3673 }
Stephen Lin837bba12013-07-15 17:55:02 +00003674
Chandler Carruthc8925912013-01-05 02:09:22 +00003675 return true;
3676}
3677
3678} // end anonymous namespace
3679
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003680/// Return true if the specified values are defined in a
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003681/// different basic block than BB.
3682static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
3683 if (Instruction *I = dyn_cast<Instruction>(V))
3684 return I->getParent() != BB;
3685 return false;
3686}
3687
Philip Reamesac115ed2016-03-09 23:13:12 +00003688/// Sink addressing mode computation immediate before MemoryInst if doing so
3689/// can be done without increasing register pressure. The need for the
3690/// register pressure constraint means this can end up being an all or nothing
3691/// decision for all uses of the same addressing computation.
3692///
Sanjay Patel4ac6b112015-09-21 22:47:23 +00003693/// Load and Store Instructions often have addressing modes that can do
3694/// significant amounts of computation. As such, instruction selection will try
3695/// to get the load or store to do as much computation as possible for the
3696/// program. The problem is that isel can only see within a single block. As
3697/// such, we sink as much legal addressing mode work into the block as possible.
Chris Lattner728f9022008-11-25 07:09:13 +00003698///
3699/// This method is used to optimize both load/store and inline asms with memory
Philip Reamesac115ed2016-03-09 23:13:12 +00003700/// operands. It's also used to sink addressing computations feeding into cold
3701/// call sites into their (cold) basic block.
3702///
3703/// The motivation for handling sinking into cold blocks is that doing so can
3704/// both enable other address mode sinking (by satisfying the register pressure
3705/// constraint above), and reduce register pressure globally (by removing the
3706/// addressing mode computation from the fast path entirely.).
Sanjay Patelfc580a62015-09-21 23:03:16 +00003707bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003708 Type *AccessTy, unsigned AddrSpace) {
Owen Anderson8ba5f392010-11-27 08:15:55 +00003709 Value *Repl = Addr;
Nadav Rotem465834c2012-07-24 10:51:42 +00003710
3711 // Try to collapse single-value PHI nodes. This is necessary to undo
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003712 // unprofitable PRE transformations.
Cameron Zwarich43cecb12011-01-03 06:33:01 +00003713 SmallVector<Value*, 8> worklist;
3714 SmallPtrSet<Value*, 16> Visited;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003715 worklist.push_back(Addr);
Nadav Rotem465834c2012-07-24 10:51:42 +00003716
Owen Anderson8ba5f392010-11-27 08:15:55 +00003717 // Use a worklist to iteratively look through PHI nodes, and ensure that
3718 // the addressing mode obtained from the non-PHI roots of the graph
3719 // are equivalent.
Craig Topperc0196b12014-04-14 00:51:57 +00003720 Value *Consensus = nullptr;
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003721 unsigned NumUsesConsensus = 0;
Cameron Zwarich13c885d2011-03-05 08:12:26 +00003722 bool IsNumUsesConsensusValid = false;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003723 SmallVector<Instruction*, 16> AddrModeInsts;
3724 ExtAddrMode AddrMode;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003725 TypePromotionTransaction TPT;
3726 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3727 TPT.getRestorationPoint();
Owen Anderson8ba5f392010-11-27 08:15:55 +00003728 while (!worklist.empty()) {
3729 Value *V = worklist.back();
3730 worklist.pop_back();
Nadav Rotem465834c2012-07-24 10:51:42 +00003731
Owen Anderson8ba5f392010-11-27 08:15:55 +00003732 // Break use-def graph loops.
David Blaikie70573dc2014-11-19 07:49:26 +00003733 if (!Visited.insert(V).second) {
Craig Topperc0196b12014-04-14 00:51:57 +00003734 Consensus = nullptr;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003735 break;
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003736 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003737
Owen Anderson8ba5f392010-11-27 08:15:55 +00003738 // For a PHI node, push all of its incoming values.
3739 if (PHINode *P = dyn_cast<PHINode>(V)) {
Pete Cooper833f34d2015-05-12 20:05:31 +00003740 for (Value *IncValue : P->incoming_values())
3741 worklist.push_back(IncValue);
Owen Anderson8ba5f392010-11-27 08:15:55 +00003742 continue;
3743 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003744
Philip Reamesac115ed2016-03-09 23:13:12 +00003745 // For non-PHIs, determine the addressing mode being computed. Note that
3746 // the result may differ depending on what other uses our candidate
3747 // addressing instructions might have.
Owen Anderson8ba5f392010-11-27 08:15:55 +00003748 SmallVector<Instruction*, 16> NewAddrModeInsts;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003749 ExtAddrMode NewAddrMode = AddressingModeMatcher::Match(
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00003750 V, AccessTy, AddrSpace, MemoryInst, NewAddrModeInsts, *TM,
Ahmed Bougachaf3299142015-06-17 20:44:32 +00003751 InsertedInsts, PromotedInsts, TPT);
Cameron Zwarich13c885d2011-03-05 08:12:26 +00003752
3753 // This check is broken into two cases with very similar code to avoid using
3754 // getNumUses() as much as possible. Some values have a lot of uses, so
3755 // calling getNumUses() unconditionally caused a significant compile-time
3756 // regression.
3757 if (!Consensus) {
3758 Consensus = V;
3759 AddrMode = NewAddrMode;
3760 AddrModeInsts = NewAddrModeInsts;
3761 continue;
3762 } else if (NewAddrMode == AddrMode) {
3763 if (!IsNumUsesConsensusValid) {
3764 NumUsesConsensus = Consensus->getNumUses();
3765 IsNumUsesConsensusValid = true;
3766 }
3767
3768 // Ensure that the obtained addressing mode is equivalent to that obtained
3769 // for all other roots of the PHI traversal. Also, when choosing one
3770 // such root as representative, select the one with the most uses in order
3771 // to keep the cost modeling heuristics in AddressingModeMatcher
3772 // applicable.
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003773 unsigned NumUses = V->getNumUses();
3774 if (NumUses > NumUsesConsensus) {
Owen Anderson8ba5f392010-11-27 08:15:55 +00003775 Consensus = V;
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003776 NumUsesConsensus = NumUses;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003777 AddrModeInsts = NewAddrModeInsts;
3778 }
3779 continue;
3780 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003781
Craig Topperc0196b12014-04-14 00:51:57 +00003782 Consensus = nullptr;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003783 break;
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003784 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003785
Owen Anderson8ba5f392010-11-27 08:15:55 +00003786 // If the addressing mode couldn't be determined, or if multiple different
3787 // ones were determined, bail out now.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003788 if (!Consensus) {
3789 TPT.rollback(LastKnownGood);
3790 return false;
3791 }
3792 TPT.commit();
Nadav Rotem465834c2012-07-24 10:51:42 +00003793
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003794 // Check to see if any of the instructions supersumed by this addr mode are
3795 // non-local to I's BB.
3796 bool AnyNonLocal = false;
3797 for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
Chris Lattner6d71b7f2008-11-26 03:20:37 +00003798 if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003799 AnyNonLocal = true;
3800 break;
3801 }
3802 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003803
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003804 // If all the instructions matched are already in this BB, don't do anything.
3805 if (!AnyNonLocal) {
David Greene74e2d492010-01-05 01:27:11 +00003806 DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003807 return false;
3808 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003809
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003810 // Insert this computation right after this user. Since our caller is
3811 // scanning from the top of the BB to the bottom, reuse of the expr are
3812 // guaranteed to happen later.
Devang Patelc10e52a2011-09-06 18:49:53 +00003813 IRBuilder<> Builder(MemoryInst);
Eric Christopherc1ea1492008-09-24 05:32:41 +00003814
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003815 // Now that we determined the addressing expression we want to use and know
3816 // that we have to sink it into this block. Check to see if we have already
3817 // done this for some other load/store instr in this block. If so, reuse the
3818 // computation.
3819 Value *&SunkAddr = SunkAddrs[Addr];
3820 if (SunkAddr) {
David Greene74e2d492010-01-05 01:27:11 +00003821 DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003822 << *MemoryInst << "\n");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003823 if (SunkAddr->getType() != Addr->getType())
Benjamin Kramer547b6c52011-09-27 20:39:19 +00003824 SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType());
Eric Christopherfccff372015-01-27 01:01:38 +00003825 } else if (AddrSinkUsingGEPs ||
3826 (!AddrSinkUsingGEPs.getNumOccurrences() && TM &&
Eric Christopher2c635492015-01-27 07:54:39 +00003827 TM->getSubtargetImpl(*MemoryInst->getParent()->getParent())
3828 ->useAA())) {
Hal Finkelc3998302014-04-12 00:59:48 +00003829 // By default, we use the GEP-based method when AA is used later. This
3830 // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities.
3831 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003832 << *MemoryInst << "\n");
Mehdi Amini4fe37982015-07-07 18:45:17 +00003833 Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
Craig Topperc0196b12014-04-14 00:51:57 +00003834 Value *ResultPtr = nullptr, *ResultIndex = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003835
3836 // First, find the pointer.
3837 if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) {
3838 ResultPtr = AddrMode.BaseReg;
Craig Topperc0196b12014-04-14 00:51:57 +00003839 AddrMode.BaseReg = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003840 }
3841
3842 if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) {
3843 // We can't add more than one pointer together, nor can we scale a
3844 // pointer (both of which seem meaningless).
3845 if (ResultPtr || AddrMode.Scale != 1)
3846 return false;
3847
3848 ResultPtr = AddrMode.ScaledReg;
3849 AddrMode.Scale = 0;
3850 }
3851
3852 if (AddrMode.BaseGV) {
3853 if (ResultPtr)
3854 return false;
3855
3856 ResultPtr = AddrMode.BaseGV;
3857 }
3858
3859 // If the real base value actually came from an inttoptr, then the matcher
3860 // will look through it and provide only the integer value. In that case,
3861 // use it here.
3862 if (!ResultPtr && AddrMode.BaseReg) {
3863 ResultPtr =
3864 Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), "sunkaddr");
Craig Topperc0196b12014-04-14 00:51:57 +00003865 AddrMode.BaseReg = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003866 } else if (!ResultPtr && AddrMode.Scale == 1) {
3867 ResultPtr =
3868 Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), "sunkaddr");
3869 AddrMode.Scale = 0;
3870 }
3871
3872 if (!ResultPtr &&
3873 !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) {
3874 SunkAddr = Constant::getNullValue(Addr->getType());
3875 } else if (!ResultPtr) {
3876 return false;
3877 } else {
3878 Type *I8PtrTy =
David Blaikie3909da72015-03-30 20:42:56 +00003879 Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace());
3880 Type *I8Ty = Builder.getInt8Ty();
Hal Finkelc3998302014-04-12 00:59:48 +00003881
3882 // Start with the base register. Do this first so that subsequent address
3883 // matching finds it last, which will prevent it from trying to match it
3884 // as the scaled value in case it happens to be a mul. That would be
3885 // problematic if we've sunk a different mul for the scale, because then
3886 // we'd end up sinking both muls.
3887 if (AddrMode.BaseReg) {
3888 Value *V = AddrMode.BaseReg;
3889 if (V->getType() != IntPtrTy)
3890 V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
3891
3892 ResultIndex = V;
3893 }
3894
3895 // Add the scale value.
3896 if (AddrMode.Scale) {
3897 Value *V = AddrMode.ScaledReg;
3898 if (V->getType() == IntPtrTy) {
3899 // done.
3900 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
3901 cast<IntegerType>(V->getType())->getBitWidth()) {
3902 V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
3903 } else {
3904 // It is only safe to sign extend the BaseReg if we know that the math
3905 // required to create it did not overflow before we extend it. Since
3906 // the original IR value was tossed in favor of a constant back when
3907 // the AddrMode was created we need to bail out gracefully if widths
3908 // do not match instead of extending it.
3909 Instruction *I = dyn_cast_or_null<Instruction>(ResultIndex);
3910 if (I && (ResultIndex != AddrMode.BaseReg))
3911 I->eraseFromParent();
3912 return false;
3913 }
3914
3915 if (AddrMode.Scale != 1)
3916 V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
3917 "sunkaddr");
3918 if (ResultIndex)
3919 ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr");
3920 else
3921 ResultIndex = V;
3922 }
3923
3924 // Add in the Base Offset if present.
3925 if (AddrMode.BaseOffs) {
3926 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
3927 if (ResultIndex) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +00003928 // We need to add this separately from the scale above to help with
3929 // SDAG consecutive load/store merging.
Hal Finkelc3998302014-04-12 00:59:48 +00003930 if (ResultPtr->getType() != I8PtrTy)
3931 ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
David Blaikie3909da72015-03-30 20:42:56 +00003932 ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
Hal Finkelc3998302014-04-12 00:59:48 +00003933 }
3934
3935 ResultIndex = V;
3936 }
3937
3938 if (!ResultIndex) {
3939 SunkAddr = ResultPtr;
3940 } else {
3941 if (ResultPtr->getType() != I8PtrTy)
3942 ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
David Blaikie3909da72015-03-30 20:42:56 +00003943 SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
Hal Finkelc3998302014-04-12 00:59:48 +00003944 }
3945
3946 if (SunkAddr->getType() != Addr->getType())
3947 SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType());
3948 }
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003949 } else {
David Greene74e2d492010-01-05 01:27:11 +00003950 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003951 << *MemoryInst << "\n");
Mehdi Amini4fe37982015-07-07 18:45:17 +00003952 Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
Craig Topperc0196b12014-04-14 00:51:57 +00003953 Value *Result = nullptr;
Dan Gohmanca194452010-01-19 22:45:06 +00003954
3955 // Start with the base register. Do this first so that subsequent address
3956 // matching finds it last, which will prevent it from trying to match it
3957 // as the scaled value in case it happens to be a mul. That would be
3958 // problematic if we've sunk a different mul for the scale, because then
3959 // we'd end up sinking both muls.
3960 if (AddrMode.BaseReg) {
3961 Value *V = AddrMode.BaseReg;
Duncan Sands19d0b472010-02-16 11:11:14 +00003962 if (V->getType()->isPointerTy())
Devang Patelc10e52a2011-09-06 18:49:53 +00003963 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
Dan Gohmanca194452010-01-19 22:45:06 +00003964 if (V->getType() != IntPtrTy)
Devang Patelc10e52a2011-09-06 18:49:53 +00003965 V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
Dan Gohmanca194452010-01-19 22:45:06 +00003966 Result = V;
3967 }
3968
3969 // Add the scale value.
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003970 if (AddrMode.Scale) {
3971 Value *V = AddrMode.ScaledReg;
3972 if (V->getType() == IntPtrTy) {
3973 // done.
Duncan Sands19d0b472010-02-16 11:11:14 +00003974 } else if (V->getType()->isPointerTy()) {
Devang Patelc10e52a2011-09-06 18:49:53 +00003975 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003976 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
3977 cast<IntegerType>(V->getType())->getBitWidth()) {
Devang Patelc10e52a2011-09-06 18:49:53 +00003978 V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003979 } else {
Jim Grosbached2cd392014-03-26 17:27:01 +00003980 // It is only safe to sign extend the BaseReg if we know that the math
3981 // required to create it did not overflow before we extend it. Since
3982 // the original IR value was tossed in favor of a constant back when
3983 // the AddrMode was created we need to bail out gracefully if widths
3984 // do not match instead of extending it.
Joey Gouly12a8bf02014-05-13 15:42:45 +00003985 Instruction *I = dyn_cast_or_null<Instruction>(Result);
Jim Grosbach83b44e12014-04-10 00:27:45 +00003986 if (I && (Result != AddrMode.BaseReg))
3987 I->eraseFromParent();
Jim Grosbached2cd392014-03-26 17:27:01 +00003988 return false;
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003989 }
3990 if (AddrMode.Scale != 1)
Devang Patelc10e52a2011-09-06 18:49:53 +00003991 V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
3992 "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003993 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00003994 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003995 else
3996 Result = V;
3997 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003998
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003999 // Add in the BaseGV if present.
4000 if (AddrMode.BaseGV) {
Devang Patelc10e52a2011-09-06 18:49:53 +00004001 Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004002 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00004003 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004004 else
4005 Result = V;
4006 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00004007
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004008 // Add in the Base Offset if present.
4009 if (AddrMode.BaseOffs) {
Owen Andersonedb4a702009-07-24 23:12:02 +00004010 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004011 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00004012 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004013 else
4014 Result = V;
4015 }
4016
Craig Topperc0196b12014-04-14 00:51:57 +00004017 if (!Result)
Owen Anderson5a1acd92009-07-31 20:28:14 +00004018 SunkAddr = Constant::getNullValue(Addr->getType());
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004019 else
Devang Patelc10e52a2011-09-06 18:49:53 +00004020 SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004021 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00004022
Owen Andersondfb8c3b2010-11-19 22:15:03 +00004023 MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
Eric Christopherc1ea1492008-09-24 05:32:41 +00004024
Chris Lattneraf1bcce2011-04-09 07:05:44 +00004025 // If we have no uses, recursively delete the value and all dead instructions
4026 // using it.
Owen Andersondfb8c3b2010-11-19 22:15:03 +00004027 if (Repl->use_empty()) {
Chris Lattneraf1bcce2011-04-09 07:05:44 +00004028 // This can cause recursive deletion, which can invalidate our iterator.
4029 // Use a WeakVH to hold onto it in case this happens.
Duncan P. N. Exon Smith7b269642016-02-21 19:37:45 +00004030 Value *CurValue = &*CurInstIterator;
4031 WeakVH IterHandle(CurValue);
Chris Lattneraf1bcce2011-04-09 07:05:44 +00004032 BasicBlock *BB = CurInstIterator->getParent();
Nadav Rotem465834c2012-07-24 10:51:42 +00004033
Benjamin Kramer8bcc9712012-08-29 15:32:21 +00004034 RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo);
Chris Lattneraf1bcce2011-04-09 07:05:44 +00004035
Duncan P. N. Exon Smith7b269642016-02-21 19:37:45 +00004036 if (IterHandle != CurValue) {
Chris Lattneraf1bcce2011-04-09 07:05:44 +00004037 // If the iterator instruction was recursively deleted, start over at the
4038 // start of the block.
4039 CurInstIterator = BB->begin();
4040 SunkAddrs.clear();
Nadav Rotem465834c2012-07-24 10:51:42 +00004041 }
Dale Johannesenb67a6e662010-03-31 20:37:15 +00004042 }
Cameron Zwarichced753f2011-01-05 17:27:27 +00004043 ++NumMemoryInsts;
Chris Lattnerfeee64e2007-04-13 20:30:56 +00004044 return true;
4045}
4046
Sanjay Patel4ac6b112015-09-21 22:47:23 +00004047/// If there are any memory operands, use OptimizeMemoryInst to sink their
4048/// address computing into the block when possible / profitable.
Sanjay Patelfc580a62015-09-21 23:03:16 +00004049bool CodeGenPrepare::optimizeInlineAsmInst(CallInst *CS) {
Evan Cheng1da25002008-02-26 02:42:37 +00004050 bool MadeChange = false;
Evan Cheng1da25002008-02-26 02:42:37 +00004051
Eric Christopher11e4df72015-02-26 22:38:43 +00004052 const TargetRegisterInfo *TRI =
4053 TM->getSubtargetImpl(*CS->getParent()->getParent())->getRegisterInfo();
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00004054 TargetLowering::AsmOperandInfoVector TargetConstraints =
4055 TLI->ParseConstraints(*DL, TRI, CS);
Dale Johannesenf95f59a2010-09-16 18:30:55 +00004056 unsigned ArgNo = 0;
John Thompson1094c802010-09-13 18:15:37 +00004057 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
4058 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
Nadav Rotem465834c2012-07-24 10:51:42 +00004059
Evan Cheng1da25002008-02-26 02:42:37 +00004060 // Compute the constraint code and ConstraintType to use.
Dale Johannesence97d552010-06-25 21:55:36 +00004061 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Evan Cheng1da25002008-02-26 02:42:37 +00004062
Eli Friedman666bbe32008-02-26 18:37:49 +00004063 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4064 OpInfo.isIndirect) {
Chris Lattner7a277142011-01-15 07:14:54 +00004065 Value *OpVal = CS->getArgOperand(ArgNo++);
Sanjay Patelfc580a62015-09-21 23:03:16 +00004066 MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->getType(), ~0u);
Dale Johannesenf95f59a2010-09-16 18:30:55 +00004067 } else if (OpInfo.Type == InlineAsm::isInput)
4068 ArgNo++;
Evan Cheng1da25002008-02-26 02:42:37 +00004069 }
4070
4071 return MadeChange;
4072}
4073
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004074/// \brief Check if all the uses of \p Inst are equivalent (or free) zero or
4075/// sign extensions.
4076static bool hasSameExtUse(Instruction *Inst, const TargetLowering &TLI) {
4077 assert(!Inst->use_empty() && "Input must have at least one use");
4078 const Instruction *FirstUser = cast<Instruction>(*Inst->user_begin());
4079 bool IsSExt = isa<SExtInst>(FirstUser);
4080 Type *ExtTy = FirstUser->getType();
4081 for (const User *U : Inst->users()) {
4082 const Instruction *UI = cast<Instruction>(U);
4083 if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI)))
4084 return false;
4085 Type *CurTy = UI->getType();
4086 // Same input and output types: Same instruction after CSE.
4087 if (CurTy == ExtTy)
4088 continue;
4089
4090 // If IsSExt is true, we are in this situation:
4091 // a = Inst
4092 // b = sext ty1 a to ty2
4093 // c = sext ty1 a to ty3
4094 // Assuming ty2 is shorter than ty3, this could be turned into:
4095 // a = Inst
4096 // b = sext ty1 a to ty2
4097 // c = sext ty2 b to ty3
4098 // However, the last sext is not free.
4099 if (IsSExt)
4100 return false;
4101
4102 // This is a ZExt, maybe this is free to extend from one type to another.
4103 // In that case, we would not account for a different use.
4104 Type *NarrowTy;
4105 Type *LargeTy;
4106 if (ExtTy->getScalarType()->getIntegerBitWidth() >
4107 CurTy->getScalarType()->getIntegerBitWidth()) {
4108 NarrowTy = CurTy;
4109 LargeTy = ExtTy;
4110 } else {
4111 NarrowTy = ExtTy;
4112 LargeTy = CurTy;
4113 }
4114
4115 if (!TLI.isZExtFree(NarrowTy, LargeTy))
4116 return false;
4117 }
4118 // All uses are the same or can be derived from one another for free.
4119 return true;
4120}
4121
4122/// \brief Try to form ExtLd by promoting \p Exts until they reach a
4123/// load instruction.
4124/// If an ext(load) can be formed, it is returned via \p LI for the load
4125/// and \p Inst for the extension.
4126/// Otherwise LI == nullptr and Inst == nullptr.
4127/// When some promotion happened, \p TPT contains the proper state to
4128/// revert them.
4129///
4130/// \return true when promoting was necessary to expose the ext(load)
4131/// opportunity, false otherwise.
4132///
4133/// Example:
4134/// \code
4135/// %ld = load i32* %addr
4136/// %add = add nuw i32 %ld, 4
4137/// %zext = zext i32 %add to i64
4138/// \endcode
4139/// =>
4140/// \code
4141/// %ld = load i32* %addr
4142/// %zext = zext i32 %ld to i64
4143/// %add = add nuw i64 %zext, 4
4144/// \encode
4145/// Thanks to the promotion, we can match zext(load i32*) to i64.
Sanjay Patelfc580a62015-09-21 23:03:16 +00004146bool CodeGenPrepare::extLdPromotion(TypePromotionTransaction &TPT,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004147 LoadInst *&LI, Instruction *&Inst,
4148 const SmallVectorImpl<Instruction *> &Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00004149 unsigned CreatedInstsCost = 0) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004150 // Iterate over all the extensions to see if one form an ext(load).
4151 for (auto I : Exts) {
4152 // Check if we directly have ext(load).
4153 if ((LI = dyn_cast<LoadInst>(I->getOperand(0)))) {
4154 Inst = I;
4155 // No promotion happened here.
4156 return false;
4157 }
4158 // Check whether or not we want to do any promotion.
4159 if (!TLI || !TLI->enableExtLdPromotion() || DisableExtLdPromotion)
4160 continue;
4161 // Get the action to perform the promotion.
4162 TypePromotionHelper::Action TPH = TypePromotionHelper::getAction(
Ahmed Bougachaf3299142015-06-17 20:44:32 +00004163 I, InsertedInsts, *TLI, PromotedInsts);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004164 // Check if we can promote.
4165 if (!TPH)
4166 continue;
4167 // Save the current state.
4168 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4169 TPT.getRestorationPoint();
4170 SmallVector<Instruction *, 4> NewExts;
Quentin Colombet1b274f92015-03-10 21:48:15 +00004171 unsigned NewCreatedInstsCost = 0;
4172 unsigned ExtCost = !TLI->isExtFree(I);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004173 // Promote.
Quentin Colombet1b274f92015-03-10 21:48:15 +00004174 Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost,
4175 &NewExts, nullptr, *TLI);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004176 assert(PromotedVal &&
4177 "TypePromotionHelper should have filtered out those cases");
4178
4179 // We would be able to merge only one extension in a load.
4180 // Therefore, if we have more than 1 new extension we heuristically
4181 // cut this search path, because it means we degrade the code quality.
4182 // With exactly 2, the transformation is neutral, because we will merge
4183 // one extension but leave one. However, we optimistically keep going,
4184 // because the new extension may be removed too.
Quentin Colombet1b274f92015-03-10 21:48:15 +00004185 long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost;
4186 TotalCreatedInstsCost -= ExtCost;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004187 if (!StressExtLdPromotion &&
Quentin Colombet1b274f92015-03-10 21:48:15 +00004188 (TotalCreatedInstsCost > 1 ||
Mehdi Amini44ede332015-07-09 02:09:04 +00004189 !isPromotedInstructionLegal(*TLI, *DL, PromotedVal))) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004190 // The promotion is not profitable, rollback to the previous state.
4191 TPT.rollback(LastKnownGood);
4192 continue;
4193 }
4194 // The promotion is profitable.
4195 // Check if it exposes an ext(load).
Sanjay Patelfc580a62015-09-21 23:03:16 +00004196 (void)extLdPromotion(TPT, LI, Inst, NewExts, TotalCreatedInstsCost);
Quentin Colombet1b274f92015-03-10 21:48:15 +00004197 if (LI && (StressExtLdPromotion || NewCreatedInstsCost <= ExtCost ||
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004198 // If we have created a new extension, i.e., now we have two
4199 // extensions. We must make sure one of them is merged with
4200 // the load, otherwise we may degrade the code quality.
4201 (LI->hasOneUse() || hasSameExtUse(LI, *TLI))))
4202 // Promotion happened.
4203 return true;
4204 // If this does not help to expose an ext(load) then, rollback.
4205 TPT.rollback(LastKnownGood);
4206 }
4207 // None of the extension can form an ext(load).
4208 LI = nullptr;
4209 Inst = nullptr;
4210 return false;
4211}
4212
Sanjay Patel4ac6b112015-09-21 22:47:23 +00004213/// Move a zext or sext fed by a load into the same basic block as the load,
4214/// unless conditions are unfavorable. This allows SelectionDAG to fold the
4215/// extend into the load.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004216/// \p I[in/out] the extension may be modified during the process if some
4217/// promotions apply.
Dan Gohman99429a02009-10-16 20:59:35 +00004218///
Sanjay Patelfc580a62015-09-21 23:03:16 +00004219bool CodeGenPrepare::moveExtToFormExtLoad(Instruction *&I) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004220 // Try to promote a chain of computation if it allows to form
4221 // an extended load.
4222 TypePromotionTransaction TPT;
4223 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4224 TPT.getRestorationPoint();
4225 SmallVector<Instruction *, 1> Exts;
4226 Exts.push_back(I);
Dan Gohman99429a02009-10-16 20:59:35 +00004227 // Look for a load being extended.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004228 LoadInst *LI = nullptr;
4229 Instruction *OldExt = I;
Sanjay Patelfc580a62015-09-21 23:03:16 +00004230 bool HasPromoted = extLdPromotion(TPT, LI, I, Exts);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004231 if (!LI || !I) {
4232 assert(!HasPromoted && !LI && "If we did not match any load instruction "
4233 "the code must remain the same");
4234 I = OldExt;
4235 return false;
4236 }
Dan Gohman99429a02009-10-16 20:59:35 +00004237
4238 // If they're already in the same block, there's nothing to do.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004239 // Make the cheap checks first if we did not promote.
4240 // If we promoted, we need to check if it is indeed profitable.
4241 if (!HasPromoted && LI->getParent() == I->getParent())
Dan Gohman99429a02009-10-16 20:59:35 +00004242 return false;
4243
Mehdi Amini44ede332015-07-09 02:09:04 +00004244 EVT VT = TLI->getValueType(*DL, I->getType());
4245 EVT LoadVT = TLI->getValueType(*DL, LI->getType());
Ahmed Bougacha55e3c2d2014-12-05 18:04:40 +00004246
Dan Gohman99429a02009-10-16 20:59:35 +00004247 // If the load has other users and the truncate is not free, this probably
4248 // isn't worthwhile.
Ahmed Bougacha55e3c2d2014-12-05 18:04:40 +00004249 if (!LI->hasOneUse() && TLI &&
4250 (TLI->isTypeLegal(LoadVT) || !TLI->isTypeLegal(VT)) &&
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004251 !TLI->isTruncateFree(I->getType(), LI->getType())) {
4252 I = OldExt;
4253 TPT.rollback(LastKnownGood);
Dan Gohman99429a02009-10-16 20:59:35 +00004254 return false;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004255 }
Dan Gohman99429a02009-10-16 20:59:35 +00004256
4257 // Check whether the target supports casts folded into loads.
4258 unsigned LType;
4259 if (isa<ZExtInst>(I))
4260 LType = ISD::ZEXTLOAD;
4261 else {
4262 assert(isa<SExtInst>(I) && "Unexpected ext type!");
4263 LType = ISD::SEXTLOAD;
4264 }
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00004265 if (TLI && !TLI->isLoadExtLegal(LType, VT, LoadVT)) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004266 I = OldExt;
4267 TPT.rollback(LastKnownGood);
Dan Gohman99429a02009-10-16 20:59:35 +00004268 return false;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004269 }
Dan Gohman99429a02009-10-16 20:59:35 +00004270
4271 // Move the extend into the same block as the load, so that SelectionDAG
4272 // can fold it.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00004273 TPT.commit();
Dan Gohman99429a02009-10-16 20:59:35 +00004274 I->removeFromParent();
4275 I->insertAfter(LI);
Cameron Zwarichced753f2011-01-05 17:27:27 +00004276 ++NumExtsMoved;
Dan Gohman99429a02009-10-16 20:59:35 +00004277 return true;
4278}
4279
Sanjay Patelfc580a62015-09-21 23:03:16 +00004280bool CodeGenPrepare::optimizeExtUses(Instruction *I) {
Evan Chengd3d80172007-12-05 23:58:20 +00004281 BasicBlock *DefBB = I->getParent();
4282
Bob Wilsonff714f92010-09-21 21:44:14 +00004283 // If the result of a {s|z}ext and its source are both live out, rewrite all
Evan Chengd3d80172007-12-05 23:58:20 +00004284 // other uses of the source with result of extension.
4285 Value *Src = I->getOperand(0);
4286 if (Src->hasOneUse())
4287 return false;
4288
Evan Cheng2011df42007-12-13 07:50:36 +00004289 // Only do this xform if truncating is free.
Gabor Greifaa261722008-02-26 19:13:21 +00004290 if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
Evan Cheng37c36ed2007-12-13 03:32:53 +00004291 return false;
4292
Evan Cheng7bc89422007-12-12 00:51:06 +00004293 // Only safe to perform the optimization if the source is also defined in
Evan Cheng63d33cf2007-12-12 02:53:41 +00004294 // this block.
4295 if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
Evan Cheng7bc89422007-12-12 00:51:06 +00004296 return false;
4297
Evan Chengd3d80172007-12-05 23:58:20 +00004298 bool DefIsLiveOut = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00004299 for (User *U : I->users()) {
4300 Instruction *UI = cast<Instruction>(U);
Evan Chengd3d80172007-12-05 23:58:20 +00004301
4302 // Figure out which BB this ext is used in.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004303 BasicBlock *UserBB = UI->getParent();
Evan Chengd3d80172007-12-05 23:58:20 +00004304 if (UserBB == DefBB) continue;
4305 DefIsLiveOut = true;
4306 break;
4307 }
4308 if (!DefIsLiveOut)
4309 return false;
4310
Jim Grosbach0f38c1e2013-04-15 17:40:48 +00004311 // Make sure none of the uses are PHI nodes.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004312 for (User *U : Src->users()) {
4313 Instruction *UI = cast<Instruction>(U);
4314 BasicBlock *UserBB = UI->getParent();
Evan Cheng37c36ed2007-12-13 03:32:53 +00004315 if (UserBB == DefBB) continue;
4316 // Be conservative. We don't want this xform to end up introducing
4317 // reloads just before load / store instructions.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004318 if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI))
Evan Cheng63d33cf2007-12-12 02:53:41 +00004319 return false;
4320 }
4321
Evan Chengd3d80172007-12-05 23:58:20 +00004322 // InsertedTruncs - Only insert one trunc in each block once.
4323 DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
4324
4325 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00004326 for (Use &U : Src->uses()) {
4327 Instruction *User = cast<Instruction>(U.getUser());
Evan Chengd3d80172007-12-05 23:58:20 +00004328
4329 // Figure out which BB this ext is used in.
4330 BasicBlock *UserBB = User->getParent();
4331 if (UserBB == DefBB) continue;
4332
4333 // Both src and def are live in this block. Rewrite the use.
4334 Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
4335
4336 if (!InsertedTrunc) {
Bill Wendling8ddfc092011-08-16 20:45:24 +00004337 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00004338 assert(InsertPt != UserBB->end());
4339 InsertedTrunc = new TruncInst(I, Src->getType(), "", &*InsertPt);
Ahmed Bougachaf3299142015-06-17 20:44:32 +00004340 InsertedInsts.insert(InsertedTrunc);
Evan Chengd3d80172007-12-05 23:58:20 +00004341 }
4342
4343 // Replace a use of the {s|z}ext source with a use of the result.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004344 U = InsertedTrunc;
Cameron Zwarichced753f2011-01-05 17:27:27 +00004345 ++NumExtUses;
Evan Chengd3d80172007-12-05 23:58:20 +00004346 MadeChange = true;
4347 }
4348
4349 return MadeChange;
4350}
4351
Geoff Berry5256fca2015-11-20 22:34:39 +00004352// Find loads whose uses only use some of the loaded value's bits. Add an "and"
4353// just after the load if the target can fold this into one extload instruction,
4354// with the hope of eliminating some of the other later "and" instructions using
4355// the loaded value. "and"s that are made trivially redundant by the insertion
4356// of the new "and" are removed by this function, while others (e.g. those whose
4357// path from the load goes through a phi) are left for isel to potentially
4358// remove.
4359//
4360// For example:
4361//
4362// b0:
4363// x = load i32
4364// ...
4365// b1:
4366// y = and x, 0xff
4367// z = use y
4368//
4369// becomes:
4370//
4371// b0:
4372// x = load i32
4373// x' = and x, 0xff
4374// ...
4375// b1:
4376// z = use x'
4377//
4378// whereas:
4379//
4380// b0:
4381// x1 = load i32
4382// ...
4383// b1:
4384// x2 = load i32
4385// ...
4386// b2:
4387// x = phi x1, x2
4388// y = and x, 0xff
4389//
4390// becomes (after a call to optimizeLoadExt for each load):
4391//
4392// b0:
4393// x1 = load i32
4394// x1' = and x1, 0xff
4395// ...
4396// b1:
4397// x2 = load i32
4398// x2' = and x2, 0xff
4399// ...
4400// b2:
4401// x = phi x1', x2'
4402// y = and x, 0xff
4403//
4404
4405bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) {
4406
4407 if (!Load->isSimple() ||
4408 !(Load->getType()->isIntegerTy() || Load->getType()->isPointerTy()))
4409 return false;
4410
4411 // Skip loads we've already transformed or have no reason to transform.
4412 if (Load->hasOneUse()) {
4413 User *LoadUser = *Load->user_begin();
4414 if (cast<Instruction>(LoadUser)->getParent() == Load->getParent() &&
4415 !dyn_cast<PHINode>(LoadUser))
4416 return false;
4417 }
4418
4419 // Look at all uses of Load, looking through phis, to determine how many bits
4420 // of the loaded value are needed.
4421 SmallVector<Instruction *, 8> WorkList;
4422 SmallPtrSet<Instruction *, 16> Visited;
4423 SmallVector<Instruction *, 8> AndsToMaybeRemove;
4424 for (auto *U : Load->users())
4425 WorkList.push_back(cast<Instruction>(U));
4426
4427 EVT LoadResultVT = TLI->getValueType(*DL, Load->getType());
4428 unsigned BitWidth = LoadResultVT.getSizeInBits();
4429 APInt DemandBits(BitWidth, 0);
4430 APInt WidestAndBits(BitWidth, 0);
4431
4432 while (!WorkList.empty()) {
4433 Instruction *I = WorkList.back();
4434 WorkList.pop_back();
4435
4436 // Break use-def graph loops.
4437 if (!Visited.insert(I).second)
4438 continue;
4439
4440 // For a PHI node, push all of its users.
4441 if (auto *Phi = dyn_cast<PHINode>(I)) {
4442 for (auto *U : Phi->users())
4443 WorkList.push_back(cast<Instruction>(U));
4444 continue;
4445 }
4446
4447 switch (I->getOpcode()) {
4448 case llvm::Instruction::And: {
4449 auto *AndC = dyn_cast<ConstantInt>(I->getOperand(1));
4450 if (!AndC)
4451 return false;
4452 APInt AndBits = AndC->getValue();
4453 DemandBits |= AndBits;
4454 // Keep track of the widest and mask we see.
4455 if (AndBits.ugt(WidestAndBits))
4456 WidestAndBits = AndBits;
4457 if (AndBits == WidestAndBits && I->getOperand(0) == Load)
4458 AndsToMaybeRemove.push_back(I);
4459 break;
4460 }
4461
4462 case llvm::Instruction::Shl: {
4463 auto *ShlC = dyn_cast<ConstantInt>(I->getOperand(1));
4464 if (!ShlC)
4465 return false;
4466 uint64_t ShiftAmt = ShlC->getLimitedValue(BitWidth - 1);
4467 auto ShlDemandBits = APInt::getAllOnesValue(BitWidth).lshr(ShiftAmt);
4468 DemandBits |= ShlDemandBits;
4469 break;
4470 }
4471
4472 case llvm::Instruction::Trunc: {
4473 EVT TruncVT = TLI->getValueType(*DL, I->getType());
4474 unsigned TruncBitWidth = TruncVT.getSizeInBits();
4475 auto TruncBits = APInt::getAllOnesValue(TruncBitWidth).zext(BitWidth);
4476 DemandBits |= TruncBits;
4477 break;
4478 }
4479
4480 default:
4481 return false;
4482 }
4483 }
4484
4485 uint32_t ActiveBits = DemandBits.getActiveBits();
4486 // Avoid hoisting (and (load x) 1) since it is unlikely to be folded by the
4487 // target even if isLoadExtLegal says an i1 EXTLOAD is valid. For example,
4488 // for the AArch64 target isLoadExtLegal(ZEXTLOAD, i32, i1) returns true, but
4489 // (and (load x) 1) is not matched as a single instruction, rather as a LDR
4490 // followed by an AND.
4491 // TODO: Look into removing this restriction by fixing backends to either
4492 // return false for isLoadExtLegal for i1 or have them select this pattern to
4493 // a single instruction.
4494 //
4495 // Also avoid hoisting if we didn't see any ands with the exact DemandBits
4496 // mask, since these are the only ands that will be removed by isel.
4497 if (ActiveBits <= 1 || !APIntOps::isMask(ActiveBits, DemandBits) ||
4498 WidestAndBits != DemandBits)
4499 return false;
4500
4501 LLVMContext &Ctx = Load->getType()->getContext();
4502 Type *TruncTy = Type::getIntNTy(Ctx, ActiveBits);
4503 EVT TruncVT = TLI->getValueType(*DL, TruncTy);
4504
4505 // Reject cases that won't be matched as extloads.
4506 if (!LoadResultVT.bitsGT(TruncVT) || !TruncVT.isRound() ||
4507 !TLI->isLoadExtLegal(ISD::ZEXTLOAD, LoadResultVT, TruncVT))
4508 return false;
4509
4510 IRBuilder<> Builder(Load->getNextNode());
4511 auto *NewAnd = dyn_cast<Instruction>(
4512 Builder.CreateAnd(Load, ConstantInt::get(Ctx, DemandBits)));
4513
4514 // Replace all uses of load with new and (except for the use of load in the
4515 // new and itself).
4516 Load->replaceAllUsesWith(NewAnd);
4517 NewAnd->setOperand(0, Load);
4518
4519 // Remove any and instructions that are now redundant.
4520 for (auto *And : AndsToMaybeRemove)
4521 // Check that the and mask is the same as the one we decided to put on the
4522 // new and.
4523 if (cast<ConstantInt>(And->getOperand(1))->getValue() == DemandBits) {
4524 And->replaceAllUsesWith(NewAnd);
4525 if (&*CurInstIterator == And)
4526 CurInstIterator = std::next(And->getIterator());
4527 And->eraseFromParent();
4528 ++NumAndUses;
4529 }
4530
4531 ++NumAndsAdded;
4532 return true;
4533}
4534
Sanjay Patel69a50a12015-10-19 21:59:12 +00004535/// Check if V (an operand of a select instruction) is an expensive instruction
4536/// that is only used once.
4537static bool sinkSelectOperand(const TargetTransformInfo *TTI, Value *V) {
4538 auto *I = dyn_cast<Instruction>(V);
4539 // If it's safe to speculatively execute, then it should not have side
4540 // effects; therefore, it's safe to sink and possibly *not* execute.
Rafael Espindola84921b92015-10-24 23:11:13 +00004541 return I && I->hasOneUse() && isSafeToSpeculativelyExecute(I) &&
4542 TTI->getUserCost(I) >= TargetTransformInfo::TCC_Expensive;
Sanjay Patel69a50a12015-10-19 21:59:12 +00004543}
4544
Sanjay Patel4ac6b112015-09-21 22:47:23 +00004545/// Returns true if a SelectInst should be turned into an explicit branch.
Sanjay Patel69a50a12015-10-19 21:59:12 +00004546static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI,
Sanjay Pateld66607b2016-04-26 17:11:17 +00004547 const TargetLowering *TLI,
Sanjay Patel69a50a12015-10-19 21:59:12 +00004548 SelectInst *SI) {
Sanjay Pateld66607b2016-04-26 17:11:17 +00004549 // If even a predictable select is cheap, then a branch can't be cheaper.
4550 if (!TLI->isPredictableSelectExpensive())
4551 return false;
4552
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004553 // FIXME: This should use the same heuristics as IfConversion to determine
Sanjay Pateld66607b2016-04-26 17:11:17 +00004554 // whether a select is better represented as a branch.
4555
4556 // If metadata tells us that the select condition is obviously predictable,
4557 // then we want to replace the select with a branch.
4558 uint64_t TrueWeight, FalseWeight;
4559 if (SI->extractProfMetadata(TrueWeight, FalseWeight)) {
4560 uint64_t Max = std::max(TrueWeight, FalseWeight);
4561 uint64_t Sum = TrueWeight + FalseWeight;
Sanjay Patelc7b91e62016-05-09 17:31:55 +00004562 if (Sum != 0) {
4563 auto Probability = BranchProbability::getBranchProbability(Max, Sum);
4564 if (Probability > TLI->getPredictableBranchThreshold())
4565 return true;
4566 }
Sanjay Pateld66607b2016-04-26 17:11:17 +00004567 }
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004568
4569 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
4570
Sanjay Patel4e652762015-09-28 22:14:51 +00004571 // If a branch is predictable, an out-of-order CPU can avoid blocking on its
4572 // comparison condition. If the compare has more than one use, there's
4573 // probably another cmov or setcc around, so it's not worth emitting a branch.
Sanjay Patel5e5f0e92015-09-28 21:44:46 +00004574 if (!Cmp || !Cmp->hasOneUse())
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004575 return false;
4576
Sanjay Patel69a50a12015-10-19 21:59:12 +00004577 // If either operand of the select is expensive and only needed on one side
4578 // of the select, we should form a branch.
4579 if (sinkSelectOperand(TTI, SI->getTrueValue()) ||
4580 sinkSelectOperand(TTI, SI->getFalseValue()))
4581 return true;
4582
4583 return false;
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004584}
4585
4586
Nadav Rotem9d832022012-09-02 12:10:19 +00004587/// If we have a SelectInst that will likely profit from branch prediction,
4588/// turn it into a branch.
Sanjay Patelfc580a62015-09-21 23:03:16 +00004589bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
Nadav Rotem9d832022012-09-02 12:10:19 +00004590 bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1);
4591
4592 // Can we convert the 'select' to CF ?
Sanjay Patela31b0c02016-04-26 00:47:39 +00004593 if (DisableSelectToBranch || OptSize || !TLI || VectorCond ||
4594 SI->getMetadata(LLVMContext::MD_unpredictable))
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004595 return false;
4596
Nadav Rotem9d832022012-09-02 12:10:19 +00004597 TargetLowering::SelectSupportKind SelectKind;
4598 if (VectorCond)
4599 SelectKind = TargetLowering::VectorMaskSelect;
4600 else if (SI->getType()->isVectorTy())
4601 SelectKind = TargetLowering::ScalarCondVectorVal;
4602 else
4603 SelectKind = TargetLowering::ScalarValSelect;
4604
Sanjay Pateld66607b2016-04-26 17:11:17 +00004605 if (TLI->isSelectSupported(SelectKind) &&
4606 !isFormingBranchFromSelectProfitable(TTI, TLI, SI))
4607 return false;
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004608
4609 ModifiedDT = true;
4610
Sanjay Patel69a50a12015-10-19 21:59:12 +00004611 // Transform a sequence like this:
4612 // start:
4613 // %cmp = cmp uge i32 %a, %b
4614 // %sel = select i1 %cmp, i32 %c, i32 %d
4615 //
4616 // Into:
4617 // start:
4618 // %cmp = cmp uge i32 %a, %b
4619 // br i1 %cmp, label %select.true, label %select.false
4620 // select.true:
4621 // br label %select.end
4622 // select.false:
4623 // br label %select.end
4624 // select.end:
4625 // %sel = phi i32 [ %c, %select.true ], [ %d, %select.false ]
4626 //
4627 // In addition, we may sink instructions that produce %c or %d from
4628 // the entry block into the destination(s) of the new branch.
4629 // If the true or false blocks do not contain a sunken instruction, that
4630 // block and its branch may be optimized away. In that case, one side of the
4631 // first branch will point directly to select.end, and the corresponding PHI
4632 // predecessor block will be the start block.
4633
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004634 // First, we split the block containing the select into 2 blocks.
4635 BasicBlock *StartBlock = SI->getParent();
4636 BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI));
Sanjay Patel69a50a12015-10-19 21:59:12 +00004637 BasicBlock *EndBlock = StartBlock->splitBasicBlock(SplitPt, "select.end");
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004638
Sanjay Patel69a50a12015-10-19 21:59:12 +00004639 // Delete the unconditional branch that was just created by the split.
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004640 StartBlock->getTerminator()->eraseFromParent();
Sanjay Patel69a50a12015-10-19 21:59:12 +00004641
4642 // These are the new basic blocks for the conditional branch.
4643 // At least one will become an actual new basic block.
4644 BasicBlock *TrueBlock = nullptr;
4645 BasicBlock *FalseBlock = nullptr;
4646
4647 // Sink expensive instructions into the conditional blocks to avoid executing
4648 // them speculatively.
4649 if (sinkSelectOperand(TTI, SI->getTrueValue())) {
4650 TrueBlock = BasicBlock::Create(SI->getContext(), "select.true.sink",
4651 EndBlock->getParent(), EndBlock);
4652 auto *TrueBranch = BranchInst::Create(EndBlock, TrueBlock);
4653 auto *TrueInst = cast<Instruction>(SI->getTrueValue());
4654 TrueInst->moveBefore(TrueBranch);
4655 }
4656 if (sinkSelectOperand(TTI, SI->getFalseValue())) {
4657 FalseBlock = BasicBlock::Create(SI->getContext(), "select.false.sink",
4658 EndBlock->getParent(), EndBlock);
4659 auto *FalseBranch = BranchInst::Create(EndBlock, FalseBlock);
4660 auto *FalseInst = cast<Instruction>(SI->getFalseValue());
4661 FalseInst->moveBefore(FalseBranch);
4662 }
4663
4664 // If there was nothing to sink, then arbitrarily choose the 'false' side
4665 // for a new input value to the PHI.
4666 if (TrueBlock == FalseBlock) {
4667 assert(TrueBlock == nullptr &&
4668 "Unexpected basic block transform while optimizing select");
4669
4670 FalseBlock = BasicBlock::Create(SI->getContext(), "select.false",
4671 EndBlock->getParent(), EndBlock);
4672 BranchInst::Create(EndBlock, FalseBlock);
4673 }
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004674
4675 // Insert the real conditional branch based on the original condition.
Sanjay Patel69a50a12015-10-19 21:59:12 +00004676 // If we did not create a new block for one of the 'true' or 'false' paths
4677 // of the condition, it means that side of the branch goes to the end block
4678 // directly and the path originates from the start block from the point of
4679 // view of the new PHI.
4680 if (TrueBlock == nullptr) {
4681 BranchInst::Create(EndBlock, FalseBlock, SI->getCondition(), SI);
4682 TrueBlock = StartBlock;
4683 } else if (FalseBlock == nullptr) {
4684 BranchInst::Create(TrueBlock, EndBlock, SI->getCondition(), SI);
4685 FalseBlock = StartBlock;
4686 } else {
4687 BranchInst::Create(TrueBlock, FalseBlock, SI->getCondition(), SI);
4688 }
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004689
4690 // The select itself is replaced with a PHI Node.
Sanjay Patel69a50a12015-10-19 21:59:12 +00004691 PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front());
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004692 PN->takeName(SI);
Sanjay Patel69a50a12015-10-19 21:59:12 +00004693 PN->addIncoming(SI->getTrueValue(), TrueBlock);
4694 PN->addIncoming(SI->getFalseValue(), FalseBlock);
4695
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004696 SI->replaceAllUsesWith(PN);
4697 SI->eraseFromParent();
4698
4699 // Instruct OptimizeBlock to skip to the next block.
4700 CurInstIterator = StartBlock->end();
4701 ++NumSelectsExpanded;
4702 return true;
4703}
4704
Benjamin Kramer573ff362014-03-01 17:24:40 +00004705static bool isBroadcastShuffle(ShuffleVectorInst *SVI) {
Tim Northoveraeb8e062014-02-19 10:02:43 +00004706 SmallVector<int, 16> Mask(SVI->getShuffleMask());
4707 int SplatElem = -1;
4708 for (unsigned i = 0; i < Mask.size(); ++i) {
4709 if (SplatElem != -1 && Mask[i] != -1 && Mask[i] != SplatElem)
4710 return false;
4711 SplatElem = Mask[i];
4712 }
4713
4714 return true;
4715}
4716
4717/// Some targets have expensive vector shifts if the lanes aren't all the same
4718/// (e.g. x86 only introduced "vpsllvd" and friends with AVX2). In these cases
4719/// it's often worth sinking a shufflevector splat down to its use so that
4720/// codegen can spot all lanes are identical.
Sanjay Patelfc580a62015-09-21 23:03:16 +00004721bool CodeGenPrepare::optimizeShuffleVectorInst(ShuffleVectorInst *SVI) {
Tim Northoveraeb8e062014-02-19 10:02:43 +00004722 BasicBlock *DefBB = SVI->getParent();
4723
4724 // Only do this xform if variable vector shifts are particularly expensive.
4725 if (!TLI || !TLI->isVectorShiftByScalarCheap(SVI->getType()))
4726 return false;
4727
4728 // We only expect better codegen by sinking a shuffle if we can recognise a
4729 // constant splat.
4730 if (!isBroadcastShuffle(SVI))
4731 return false;
4732
4733 // InsertedShuffles - Only insert a shuffle in each block once.
4734 DenseMap<BasicBlock*, Instruction*> InsertedShuffles;
4735
4736 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00004737 for (User *U : SVI->users()) {
4738 Instruction *UI = cast<Instruction>(U);
Tim Northoveraeb8e062014-02-19 10:02:43 +00004739
4740 // Figure out which BB this ext is used in.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004741 BasicBlock *UserBB = UI->getParent();
Tim Northoveraeb8e062014-02-19 10:02:43 +00004742 if (UserBB == DefBB) continue;
4743
4744 // For now only apply this when the splat is used by a shift instruction.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004745 if (!UI->isShift()) continue;
Tim Northoveraeb8e062014-02-19 10:02:43 +00004746
4747 // Everything checks out, sink the shuffle if the user's block doesn't
4748 // already have a copy.
4749 Instruction *&InsertedShuffle = InsertedShuffles[UserBB];
4750
4751 if (!InsertedShuffle) {
4752 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00004753 assert(InsertPt != UserBB->end());
4754 InsertedShuffle =
4755 new ShuffleVectorInst(SVI->getOperand(0), SVI->getOperand(1),
4756 SVI->getOperand(2), "", &*InsertPt);
Tim Northoveraeb8e062014-02-19 10:02:43 +00004757 }
4758
Chandler Carruthcdf47882014-03-09 03:16:01 +00004759 UI->replaceUsesOfWith(SVI, InsertedShuffle);
Tim Northoveraeb8e062014-02-19 10:02:43 +00004760 MadeChange = true;
4761 }
4762
4763 // If we removed all uses, nuke the shuffle.
4764 if (SVI->use_empty()) {
4765 SVI->eraseFromParent();
4766 MadeChange = true;
4767 }
4768
4769 return MadeChange;
4770}
4771
Sanjay Patel0ed9aea2015-11-02 23:22:49 +00004772bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) {
4773 if (!TLI || !DL)
4774 return false;
4775
4776 Value *Cond = SI->getCondition();
4777 Type *OldType = Cond->getType();
4778 LLVMContext &Context = Cond->getContext();
4779 MVT RegType = TLI->getRegisterType(Context, TLI->getValueType(*DL, OldType));
4780 unsigned RegWidth = RegType.getSizeInBits();
4781
4782 if (RegWidth <= cast<IntegerType>(OldType)->getBitWidth())
4783 return false;
4784
4785 // If the register width is greater than the type width, expand the condition
4786 // of the switch instruction and each case constant to the width of the
4787 // register. By widening the type of the switch condition, subsequent
4788 // comparisons (for case comparisons) will not need to be extended to the
4789 // preferred register width, so we will potentially eliminate N-1 extends,
4790 // where N is the number of cases in the switch.
4791 auto *NewType = Type::getIntNTy(Context, RegWidth);
4792
4793 // Zero-extend the switch condition and case constants unless the switch
4794 // condition is a function argument that is already being sign-extended.
4795 // In that case, we can avoid an unnecessary mask/extension by sign-extending
4796 // everything instead.
4797 Instruction::CastOps ExtType = Instruction::ZExt;
4798 if (auto *Arg = dyn_cast<Argument>(Cond))
4799 if (Arg->hasSExtAttr())
4800 ExtType = Instruction::SExt;
4801
4802 auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
4803 ExtInst->insertBefore(SI);
4804 SI->setCondition(ExtInst);
4805 for (SwitchInst::CaseIt Case : SI->cases()) {
4806 APInt NarrowConst = Case.getCaseValue()->getValue();
4807 APInt WideConst = (ExtType == Instruction::ZExt) ?
4808 NarrowConst.zext(RegWidth) : NarrowConst.sext(RegWidth);
4809 Case.setValue(ConstantInt::get(Context, WideConst));
4810 }
4811
4812 return true;
4813}
4814
Quentin Colombetc32615d2014-10-31 17:52:53 +00004815namespace {
4816/// \brief Helper class to promote a scalar operation to a vector one.
4817/// This class is used to move downward extractelement transition.
4818/// E.g.,
4819/// a = vector_op <2 x i32>
4820/// b = extractelement <2 x i32> a, i32 0
4821/// c = scalar_op b
4822/// store c
4823///
4824/// =>
4825/// a = vector_op <2 x i32>
4826/// c = vector_op a (equivalent to scalar_op on the related lane)
4827/// * d = extractelement <2 x i32> c, i32 0
4828/// * store d
4829/// Assuming both extractelement and store can be combine, we get rid of the
4830/// transition.
4831class VectorPromoteHelper {
Mehdi Amini44ede332015-07-09 02:09:04 +00004832 /// DataLayout associated with the current module.
4833 const DataLayout &DL;
4834
Quentin Colombetc32615d2014-10-31 17:52:53 +00004835 /// Used to perform some checks on the legality of vector operations.
4836 const TargetLowering &TLI;
4837
4838 /// Used to estimated the cost of the promoted chain.
4839 const TargetTransformInfo &TTI;
4840
4841 /// The transition being moved downwards.
4842 Instruction *Transition;
4843 /// The sequence of instructions to be promoted.
4844 SmallVector<Instruction *, 4> InstsToBePromoted;
4845 /// Cost of combining a store and an extract.
4846 unsigned StoreExtractCombineCost;
4847 /// Instruction that will be combined with the transition.
4848 Instruction *CombineInst;
4849
4850 /// \brief The instruction that represents the current end of the transition.
4851 /// Since we are faking the promotion until we reach the end of the chain
4852 /// of computation, we need a way to get the current end of the transition.
4853 Instruction *getEndOfTransition() const {
4854 if (InstsToBePromoted.empty())
4855 return Transition;
4856 return InstsToBePromoted.back();
4857 }
4858
4859 /// \brief Return the index of the original value in the transition.
4860 /// E.g., for "extractelement <2 x i32> c, i32 1" the original value,
4861 /// c, is at index 0.
4862 unsigned getTransitionOriginalValueIdx() const {
4863 assert(isa<ExtractElementInst>(Transition) &&
4864 "Other kind of transitions are not supported yet");
4865 return 0;
4866 }
4867
4868 /// \brief Return the index of the index in the transition.
4869 /// E.g., for "extractelement <2 x i32> c, i32 0" the index
4870 /// is at index 1.
4871 unsigned getTransitionIdx() const {
4872 assert(isa<ExtractElementInst>(Transition) &&
4873 "Other kind of transitions are not supported yet");
4874 return 1;
4875 }
4876
4877 /// \brief Get the type of the transition.
4878 /// This is the type of the original value.
4879 /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the
4880 /// transition is <2 x i32>.
4881 Type *getTransitionType() const {
4882 return Transition->getOperand(getTransitionOriginalValueIdx())->getType();
4883 }
4884
4885 /// \brief Promote \p ToBePromoted by moving \p Def downward through.
4886 /// I.e., we have the following sequence:
4887 /// Def = Transition <ty1> a to <ty2>
4888 /// b = ToBePromoted <ty2> Def, ...
4889 /// =>
4890 /// b = ToBePromoted <ty1> a, ...
4891 /// Def = Transition <ty1> ToBePromoted to <ty2>
4892 void promoteImpl(Instruction *ToBePromoted);
4893
4894 /// \brief Check whether or not it is profitable to promote all the
4895 /// instructions enqueued to be promoted.
4896 bool isProfitableToPromote() {
4897 Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx());
4898 unsigned Index = isa<ConstantInt>(ValIdx)
4899 ? cast<ConstantInt>(ValIdx)->getZExtValue()
4900 : -1;
4901 Type *PromotedType = getTransitionType();
4902
4903 StoreInst *ST = cast<StoreInst>(CombineInst);
4904 unsigned AS = ST->getPointerAddressSpace();
4905 unsigned Align = ST->getAlignment();
4906 // Check if this store is supported.
4907 if (!TLI.allowsMisalignedMemoryAccesses(
Mehdi Amini44ede332015-07-09 02:09:04 +00004908 TLI.getValueType(DL, ST->getValueOperand()->getType()), AS,
4909 Align)) {
Quentin Colombetc32615d2014-10-31 17:52:53 +00004910 // If this is not supported, there is no way we can combine
4911 // the extract with the store.
4912 return false;
4913 }
4914
4915 // The scalar chain of computation has to pay for the transition
4916 // scalar to vector.
4917 // The vector chain has to account for the combining cost.
4918 uint64_t ScalarCost =
4919 TTI.getVectorInstrCost(Transition->getOpcode(), PromotedType, Index);
4920 uint64_t VectorCost = StoreExtractCombineCost;
4921 for (const auto &Inst : InstsToBePromoted) {
4922 // Compute the cost.
4923 // By construction, all instructions being promoted are arithmetic ones.
4924 // Moreover, one argument is a constant that can be viewed as a splat
4925 // constant.
4926 Value *Arg0 = Inst->getOperand(0);
4927 bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) ||
4928 isa<ConstantFP>(Arg0);
4929 TargetTransformInfo::OperandValueKind Arg0OVK =
4930 IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
4931 : TargetTransformInfo::OK_AnyValue;
4932 TargetTransformInfo::OperandValueKind Arg1OVK =
4933 !IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
4934 : TargetTransformInfo::OK_AnyValue;
4935 ScalarCost += TTI.getArithmeticInstrCost(
4936 Inst->getOpcode(), Inst->getType(), Arg0OVK, Arg1OVK);
4937 VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType,
4938 Arg0OVK, Arg1OVK);
4939 }
4940 DEBUG(dbgs() << "Estimated cost of computation to be promoted:\nScalar: "
4941 << ScalarCost << "\nVector: " << VectorCost << '\n');
4942 return ScalarCost > VectorCost;
4943 }
4944
4945 /// \brief Generate a constant vector with \p Val with the same
4946 /// number of elements as the transition.
4947 /// \p UseSplat defines whether or not \p Val should be replicated
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004948 /// across the whole vector.
Quentin Colombetc32615d2014-10-31 17:52:53 +00004949 /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>,
4950 /// otherwise we generate a vector with as many undef as possible:
4951 /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only
4952 /// used at the index of the extract.
4953 Value *getConstantVector(Constant *Val, bool UseSplat) const {
4954 unsigned ExtractIdx = UINT_MAX;
4955 if (!UseSplat) {
4956 // If we cannot determine where the constant must be, we have to
4957 // use a splat constant.
4958 Value *ValExtractIdx = Transition->getOperand(getTransitionIdx());
4959 if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx))
4960 ExtractIdx = CstVal->getSExtValue();
4961 else
4962 UseSplat = true;
4963 }
4964
4965 unsigned End = getTransitionType()->getVectorNumElements();
4966 if (UseSplat)
4967 return ConstantVector::getSplat(End, Val);
4968
4969 SmallVector<Constant *, 4> ConstVec;
4970 UndefValue *UndefVal = UndefValue::get(Val->getType());
4971 for (unsigned Idx = 0; Idx != End; ++Idx) {
4972 if (Idx == ExtractIdx)
4973 ConstVec.push_back(Val);
4974 else
4975 ConstVec.push_back(UndefVal);
4976 }
4977 return ConstantVector::get(ConstVec);
4978 }
4979
4980 /// \brief Check if promoting to a vector type an operand at \p OperandIdx
4981 /// in \p Use can trigger undefined behavior.
4982 static bool canCauseUndefinedBehavior(const Instruction *Use,
4983 unsigned OperandIdx) {
4984 // This is not safe to introduce undef when the operand is on
4985 // the right hand side of a division-like instruction.
4986 if (OperandIdx != 1)
4987 return false;
4988 switch (Use->getOpcode()) {
4989 default:
4990 return false;
4991 case Instruction::SDiv:
4992 case Instruction::UDiv:
4993 case Instruction::SRem:
4994 case Instruction::URem:
4995 return true;
4996 case Instruction::FDiv:
4997 case Instruction::FRem:
4998 return !Use->hasNoNaNs();
4999 }
5000 llvm_unreachable(nullptr);
5001 }
5002
5003public:
Mehdi Amini44ede332015-07-09 02:09:04 +00005004 VectorPromoteHelper(const DataLayout &DL, const TargetLowering &TLI,
5005 const TargetTransformInfo &TTI, Instruction *Transition,
5006 unsigned CombineCost)
5007 : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition),
Quentin Colombetc32615d2014-10-31 17:52:53 +00005008 StoreExtractCombineCost(CombineCost), CombineInst(nullptr) {
5009 assert(Transition && "Do not know how to promote null");
5010 }
5011
5012 /// \brief Check if we can promote \p ToBePromoted to \p Type.
5013 bool canPromote(const Instruction *ToBePromoted) const {
5014 // We could support CastInst too.
5015 return isa<BinaryOperator>(ToBePromoted);
5016 }
5017
5018 /// \brief Check if it is profitable to promote \p ToBePromoted
5019 /// by moving downward the transition through.
5020 bool shouldPromote(const Instruction *ToBePromoted) const {
5021 // Promote only if all the operands can be statically expanded.
5022 // Indeed, we do not want to introduce any new kind of transitions.
5023 for (const Use &U : ToBePromoted->operands()) {
5024 const Value *Val = U.get();
5025 if (Val == getEndOfTransition()) {
5026 // If the use is a division and the transition is on the rhs,
5027 // we cannot promote the operation, otherwise we may create a
5028 // division by zero.
5029 if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()))
5030 return false;
5031 continue;
5032 }
5033 if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) &&
5034 !isa<ConstantFP>(Val))
5035 return false;
5036 }
5037 // Check that the resulting operation is legal.
5038 int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode());
5039 if (!ISDOpcode)
5040 return false;
5041 return StressStoreExtract ||
Ahmed Bougacha026600d2014-11-12 23:05:03 +00005042 TLI.isOperationLegalOrCustom(
Mehdi Amini44ede332015-07-09 02:09:04 +00005043 ISDOpcode, TLI.getValueType(DL, getTransitionType(), true));
Quentin Colombetc32615d2014-10-31 17:52:53 +00005044 }
5045
5046 /// \brief Check whether or not \p Use can be combined
5047 /// with the transition.
5048 /// I.e., is it possible to do Use(Transition) => AnotherUse?
5049 bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); }
5050
5051 /// \brief Record \p ToBePromoted as part of the chain to be promoted.
5052 void enqueueForPromotion(Instruction *ToBePromoted) {
5053 InstsToBePromoted.push_back(ToBePromoted);
5054 }
5055
5056 /// \brief Set the instruction that will be combined with the transition.
5057 void recordCombineInstruction(Instruction *ToBeCombined) {
5058 assert(canCombine(ToBeCombined) && "Unsupported instruction to combine");
5059 CombineInst = ToBeCombined;
5060 }
5061
5062 /// \brief Promote all the instructions enqueued for promotion if it is
5063 /// is profitable.
5064 /// \return True if the promotion happened, false otherwise.
5065 bool promote() {
5066 // Check if there is something to promote.
5067 // Right now, if we do not have anything to combine with,
5068 // we assume the promotion is not profitable.
5069 if (InstsToBePromoted.empty() || !CombineInst)
5070 return false;
5071
5072 // Check cost.
5073 if (!StressStoreExtract && !isProfitableToPromote())
5074 return false;
5075
5076 // Promote.
5077 for (auto &ToBePromoted : InstsToBePromoted)
5078 promoteImpl(ToBePromoted);
5079 InstsToBePromoted.clear();
5080 return true;
5081 }
5082};
5083} // End of anonymous namespace.
5084
5085void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) {
5086 // At this point, we know that all the operands of ToBePromoted but Def
5087 // can be statically promoted.
5088 // For Def, we need to use its parameter in ToBePromoted:
5089 // b = ToBePromoted ty1 a
5090 // Def = Transition ty1 b to ty2
5091 // Move the transition down.
5092 // 1. Replace all uses of the promoted operation by the transition.
5093 // = ... b => = ... Def.
5094 assert(ToBePromoted->getType() == Transition->getType() &&
5095 "The type of the result of the transition does not match "
5096 "the final type");
5097 ToBePromoted->replaceAllUsesWith(Transition);
5098 // 2. Update the type of the uses.
5099 // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def.
5100 Type *TransitionTy = getTransitionType();
5101 ToBePromoted->mutateType(TransitionTy);
5102 // 3. Update all the operands of the promoted operation with promoted
5103 // operands.
5104 // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a.
5105 for (Use &U : ToBePromoted->operands()) {
5106 Value *Val = U.get();
5107 Value *NewVal = nullptr;
5108 if (Val == Transition)
5109 NewVal = Transition->getOperand(getTransitionOriginalValueIdx());
5110 else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) ||
5111 isa<ConstantFP>(Val)) {
5112 // Use a splat constant if it is not safe to use undef.
5113 NewVal = getConstantVector(
5114 cast<Constant>(Val),
5115 isa<UndefValue>(Val) ||
5116 canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()));
5117 } else
Craig Topperd3c02f12015-01-05 10:15:49 +00005118 llvm_unreachable("Did you modified shouldPromote and forgot to update "
5119 "this?");
Quentin Colombetc32615d2014-10-31 17:52:53 +00005120 ToBePromoted->setOperand(U.getOperandNo(), NewVal);
5121 }
5122 Transition->removeFromParent();
5123 Transition->insertAfter(ToBePromoted);
5124 Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted);
5125}
5126
5127/// Some targets can do store(extractelement) with one instruction.
5128/// Try to push the extractelement towards the stores when the target
5129/// has this feature and this is profitable.
Sanjay Patelfc580a62015-09-21 23:03:16 +00005130bool CodeGenPrepare::optimizeExtractElementInst(Instruction *Inst) {
Quentin Colombetc32615d2014-10-31 17:52:53 +00005131 unsigned CombineCost = UINT_MAX;
5132 if (DisableStoreExtract || !TLI ||
5133 (!StressStoreExtract &&
5134 !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(),
5135 Inst->getOperand(1), CombineCost)))
5136 return false;
5137
5138 // At this point we know that Inst is a vector to scalar transition.
5139 // Try to move it down the def-use chain, until:
5140 // - We can combine the transition with its single use
5141 // => we got rid of the transition.
5142 // - We escape the current basic block
5143 // => we would need to check that we are moving it at a cheaper place and
5144 // we do not do that for now.
5145 BasicBlock *Parent = Inst->getParent();
5146 DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n');
Mehdi Amini44ede332015-07-09 02:09:04 +00005147 VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost);
Quentin Colombetc32615d2014-10-31 17:52:53 +00005148 // If the transition has more than one use, assume this is not going to be
5149 // beneficial.
5150 while (Inst->hasOneUse()) {
5151 Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin());
5152 DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n');
5153
5154 if (ToBePromoted->getParent() != Parent) {
5155 DEBUG(dbgs() << "Instruction to promote is in a different block ("
5156 << ToBePromoted->getParent()->getName()
5157 << ") than the transition (" << Parent->getName() << ").\n");
5158 return false;
5159 }
5160
5161 if (VPH.canCombine(ToBePromoted)) {
5162 DEBUG(dbgs() << "Assume " << *Inst << '\n'
5163 << "will be combined with: " << *ToBePromoted << '\n');
5164 VPH.recordCombineInstruction(ToBePromoted);
5165 bool Changed = VPH.promote();
5166 NumStoreExtractExposed += Changed;
5167 return Changed;
5168 }
5169
5170 DEBUG(dbgs() << "Try promoting.\n");
5171 if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted))
5172 return false;
5173
5174 DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n");
5175
5176 VPH.enqueueForPromotion(ToBePromoted);
5177 Inst = ToBePromoted;
5178 }
5179 return false;
5180}
5181
Sanjay Patelfc580a62015-09-21 23:03:16 +00005182bool CodeGenPrepare::optimizeInst(Instruction *I, bool& ModifiedDT) {
Ahmed Bougachaf3299142015-06-17 20:44:32 +00005183 // Bail out if we inserted the instruction to prevent optimizations from
5184 // stepping on each other's toes.
5185 if (InsertedInsts.count(I))
5186 return false;
5187
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005188 if (PHINode *P = dyn_cast<PHINode>(I)) {
5189 // It is possible for very late stage optimizations (such as SimplifyCFG)
5190 // to introduce PHI nodes too late to be cleaned up. If we detect such a
5191 // trivial PHI, go ahead and zap it here.
Mehdi Amini4fe37982015-07-07 18:45:17 +00005192 if (Value *V = SimplifyInstruction(P, *DL, TLInfo, nullptr)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005193 P->replaceAllUsesWith(V);
5194 P->eraseFromParent();
5195 ++NumPHIsElim;
Chris Lattneree588de2011-01-15 07:29:01 +00005196 return true;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005197 }
Chris Lattneree588de2011-01-15 07:29:01 +00005198 return false;
5199 }
Nadav Rotem465834c2012-07-24 10:51:42 +00005200
Chris Lattneree588de2011-01-15 07:29:01 +00005201 if (CastInst *CI = dyn_cast<CastInst>(I)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005202 // If the source of the cast is a constant, then this should have
5203 // already been constant folded. The only reason NOT to constant fold
5204 // it is if something (e.g. LSR) was careful to place the constant
5205 // evaluation in a block other than then one that uses it (e.g. to hoist
5206 // the address of globals out of a loop). If this is the case, we don't
5207 // want to forward-subst the cast.
5208 if (isa<Constant>(CI->getOperand(0)))
5209 return false;
5210
Mehdi Amini44ede332015-07-09 02:09:04 +00005211 if (TLI && OptimizeNoopCopyExpression(CI, *TLI, *DL))
Chris Lattneree588de2011-01-15 07:29:01 +00005212 return true;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005213
Chris Lattneree588de2011-01-15 07:29:01 +00005214 if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
Manuel Jacoba7c48f92014-03-13 13:36:25 +00005215 /// Sink a zext or sext into its user blocks if the target type doesn't
5216 /// fit in one register
Mehdi Amini44ede332015-07-09 02:09:04 +00005217 if (TLI &&
5218 TLI->getTypeAction(CI->getContext(),
5219 TLI->getValueType(*DL, CI->getType())) ==
5220 TargetLowering::TypeExpandInteger) {
Manuel Jacoba7c48f92014-03-13 13:36:25 +00005221 return SinkCast(CI);
5222 } else {
Sanjay Patelfc580a62015-09-21 23:03:16 +00005223 bool MadeChange = moveExtToFormExtLoad(I);
5224 return MadeChange | optimizeExtUses(I);
Manuel Jacoba7c48f92014-03-13 13:36:25 +00005225 }
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005226 }
Chris Lattneree588de2011-01-15 07:29:01 +00005227 return false;
5228 }
Nadav Rotem465834c2012-07-24 10:51:42 +00005229
Chris Lattneree588de2011-01-15 07:29:01 +00005230 if (CmpInst *CI = dyn_cast<CmpInst>(I))
Hal Finkeldecb0242014-01-02 21:13:43 +00005231 if (!TLI || !TLI->hasMultipleConditionRegisters())
Peter Zotovf87e5502016-04-03 17:11:53 +00005232 return OptimizeCmpExpression(CI, TLI);
Nadav Rotem465834c2012-07-24 10:51:42 +00005233
Chris Lattneree588de2011-01-15 07:29:01 +00005234 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Piotr Padlewski6c15ec42015-09-15 18:32:14 +00005235 stripInvariantGroupMetadata(*LI);
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00005236 if (TLI) {
Geoff Berry5256fca2015-11-20 22:34:39 +00005237 bool Modified = optimizeLoadExt(LI);
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00005238 unsigned AS = LI->getPointerAddressSpace();
Geoff Berry5256fca2015-11-20 22:34:39 +00005239 Modified |= optimizeMemoryInst(I, I->getOperand(0), LI->getType(), AS);
5240 return Modified;
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00005241 }
Hans Wennborgf3254832012-10-30 11:23:25 +00005242 return false;
Chris Lattneree588de2011-01-15 07:29:01 +00005243 }
Nadav Rotem465834c2012-07-24 10:51:42 +00005244
Chris Lattneree588de2011-01-15 07:29:01 +00005245 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Piotr Padlewski6c15ec42015-09-15 18:32:14 +00005246 stripInvariantGroupMetadata(*SI);
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00005247 if (TLI) {
5248 unsigned AS = SI->getPointerAddressSpace();
Sanjay Patelfc580a62015-09-21 23:03:16 +00005249 return optimizeMemoryInst(I, SI->getOperand(1),
Matt Arsenaultf72b49b2015-06-04 16:17:38 +00005250 SI->getOperand(0)->getType(), AS);
5251 }
Chris Lattneree588de2011-01-15 07:29:01 +00005252 return false;
5253 }
Nadav Rotem465834c2012-07-24 10:51:42 +00005254
Yi Jiangd069f632014-04-21 19:34:27 +00005255 BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I);
5256
5257 if (BinOp && (BinOp->getOpcode() == Instruction::AShr ||
5258 BinOp->getOpcode() == Instruction::LShr)) {
5259 ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1));
5260 if (TLI && CI && TLI->hasExtractBitsInsn())
Mehdi Amini44ede332015-07-09 02:09:04 +00005261 return OptimizeExtractBits(BinOp, CI, *TLI, *DL);
Yi Jiangd069f632014-04-21 19:34:27 +00005262
5263 return false;
5264 }
5265
Chris Lattneree588de2011-01-15 07:29:01 +00005266 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
Cameron Zwarichd28c78e2011-01-06 02:44:52 +00005267 if (GEPI->hasAllZeroIndices()) {
5268 /// The GEP operand must be a pointer, so must its result -> BitCast
5269 Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
5270 GEPI->getName(), GEPI);
5271 GEPI->replaceAllUsesWith(NC);
5272 GEPI->eraseFromParent();
5273 ++NumGEPsElim;
Sanjay Patelfc580a62015-09-21 23:03:16 +00005274 optimizeInst(NC, ModifiedDT);
Chris Lattneree588de2011-01-15 07:29:01 +00005275 return true;
Cameron Zwarichd28c78e2011-01-06 02:44:52 +00005276 }
Chris Lattneree588de2011-01-15 07:29:01 +00005277 return false;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005278 }
Nadav Rotem465834c2012-07-24 10:51:42 +00005279
Chris Lattneree588de2011-01-15 07:29:01 +00005280 if (CallInst *CI = dyn_cast<CallInst>(I))
Sanjay Patelfc580a62015-09-21 23:03:16 +00005281 return optimizeCallInst(CI, ModifiedDT);
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005282
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00005283 if (SelectInst *SI = dyn_cast<SelectInst>(I))
Sanjay Patelfc580a62015-09-21 23:03:16 +00005284 return optimizeSelectInst(SI);
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00005285
Tim Northoveraeb8e062014-02-19 10:02:43 +00005286 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I))
Sanjay Patelfc580a62015-09-21 23:03:16 +00005287 return optimizeShuffleVectorInst(SVI);
Tim Northoveraeb8e062014-02-19 10:02:43 +00005288
Sanjay Patel0ed9aea2015-11-02 23:22:49 +00005289 if (auto *Switch = dyn_cast<SwitchInst>(I))
5290 return optimizeSwitchInst(Switch);
5291
Quentin Colombetc32615d2014-10-31 17:52:53 +00005292 if (isa<ExtractElementInst>(I))
Sanjay Patelfc580a62015-09-21 23:03:16 +00005293 return optimizeExtractElementInst(I);
Quentin Colombetc32615d2014-10-31 17:52:53 +00005294
Chris Lattneree588de2011-01-15 07:29:01 +00005295 return false;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00005296}
5297
James Molloyf01488e2016-01-15 09:20:19 +00005298/// Given an OR instruction, check to see if this is a bitreverse
5299/// idiom. If so, insert the new intrinsic and return true.
5300static bool makeBitReverse(Instruction &I, const DataLayout &DL,
5301 const TargetLowering &TLI) {
5302 if (!I.getType()->isIntegerTy() ||
5303 !TLI.isOperationLegalOrCustom(ISD::BITREVERSE,
5304 TLI.getValueType(DL, I.getType(), true)))
5305 return false;
5306
5307 SmallVector<Instruction*, 4> Insts;
Chad Rosiera00df492016-05-25 16:22:14 +00005308 if (!recognizeBSwapOrBitReverseIdiom(&I, false, true, Insts))
James Molloyf01488e2016-01-15 09:20:19 +00005309 return false;
5310 Instruction *LastInst = Insts.back();
5311 I.replaceAllUsesWith(LastInst);
5312 RecursivelyDeleteTriviallyDeadInstructions(&I);
5313 return true;
5314}
5315
Chris Lattnerf2836d12007-03-31 04:06:36 +00005316// In this pass we look for GEP and cast instructions that are used
5317// across basic blocks and rewrite them to improve basic-block-at-a-time
5318// selection.
Sanjay Patelfc580a62015-09-21 23:03:16 +00005319bool CodeGenPrepare::optimizeBlock(BasicBlock &BB, bool& ModifiedDT) {
Cameron Zwarichce3b9302011-01-06 00:42:50 +00005320 SunkAddrs.clear();
Cameron Zwarich5dd2aa22011-03-02 03:31:46 +00005321 bool MadeChange = false;
Eric Christopherc1ea1492008-09-24 05:32:41 +00005322
Chris Lattner7a277142011-01-15 07:14:54 +00005323 CurInstIterator = BB.begin();
Elena Demikhovsky87700a72014-12-28 08:54:45 +00005324 while (CurInstIterator != BB.end()) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00005325 MadeChange |= optimizeInst(&*CurInstIterator++, ModifiedDT);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00005326 if (ModifiedDT)
5327 return true;
5328 }
Benjamin Kramer455fa352012-11-23 19:17:06 +00005329
James Molloyf01488e2016-01-15 09:20:19 +00005330 bool MadeBitReverse = true;
5331 while (TLI && MadeBitReverse) {
5332 MadeBitReverse = false;
5333 for (auto &I : reverse(BB)) {
5334 if (makeBitReverse(I, *DL, *TLI)) {
5335 MadeBitReverse = MadeChange = true;
George Burgess IVd4febd12016-03-22 21:25:08 +00005336 ModifiedDT = true;
James Molloyf01488e2016-01-15 09:20:19 +00005337 break;
5338 }
5339 }
5340 }
James Molloy3ef84c42016-01-15 10:36:01 +00005341 MadeChange |= dupRetToEnableTailCallOpts(&BB);
Junmo Park7d6c5f12016-01-28 09:42:39 +00005342
Chris Lattnerf2836d12007-03-31 04:06:36 +00005343 return MadeChange;
5344}
Devang Patel53771ba2011-08-18 00:50:51 +00005345
5346// llvm.dbg.value is far away from the value then iSel may not be able
Nadav Rotem465834c2012-07-24 10:51:42 +00005347// handle it properly. iSel will drop llvm.dbg.value if it can not
Devang Patel53771ba2011-08-18 00:50:51 +00005348// find a node corresponding to the value.
Sanjay Patelfc580a62015-09-21 23:03:16 +00005349bool CodeGenPrepare::placeDbgValues(Function &F) {
Devang Patel53771ba2011-08-18 00:50:51 +00005350 bool MadeChange = false;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +00005351 for (BasicBlock &BB : F) {
Craig Topperc0196b12014-04-14 00:51:57 +00005352 Instruction *PrevNonDbgInst = nullptr;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +00005353 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +00005354 Instruction *Insn = &*BI++;
Devang Patel53771ba2011-08-18 00:50:51 +00005355 DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn);
Adrian Prantl32da8892014-04-25 20:49:25 +00005356 // Leave dbg.values that refer to an alloca alone. These
5357 // instrinsics describe the address of a variable (= the alloca)
5358 // being taken. They should not be moved next to the alloca
5359 // (and to the beginning of the scope), but rather stay close to
5360 // where said address is used.
5361 if (!DVI || (DVI->getValue() && isa<AllocaInst>(DVI->getValue()))) {
Devang Patel53771ba2011-08-18 00:50:51 +00005362 PrevNonDbgInst = Insn;
5363 continue;
5364 }
5365
5366 Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue());
5367 if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) {
Reid Kleckner8de1fe22015-12-08 23:00:03 +00005368 // If VI is a phi in a block with an EHPad terminator, we can't insert
5369 // after it.
5370 if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad())
5371 continue;
Devang Patel53771ba2011-08-18 00:50:51 +00005372 DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI);
5373 DVI->removeFromParent();
Reid Klecknere18f92b2015-12-08 22:33:23 +00005374 if (isa<PHINode>(VI))
5375 DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt());
5376 else
5377 DVI->insertAfter(VI);
Devang Patel53771ba2011-08-18 00:50:51 +00005378 MadeChange = true;
5379 ++NumDbgValueMoved;
5380 }
5381 }
5382 }
5383 return MadeChange;
5384}
Tim Northovercea0abb2014-03-29 08:22:29 +00005385
5386// If there is a sequence that branches based on comparing a single bit
5387// against zero that can be combined into a single instruction, and the
5388// target supports folding these into a single instruction, sink the
5389// mask and compare into the branch uses. Do this before OptimizeBlock ->
5390// OptimizeInst -> OptimizeCmpExpression, which perturbs the pattern being
5391// searched for.
5392bool CodeGenPrepare::sinkAndCmp(Function &F) {
5393 if (!EnableAndCmpSinking)
5394 return false;
5395 if (!TLI || !TLI->isMaskAndBranchFoldingLegal())
5396 return false;
5397 bool MadeChange = false;
Sanjay Patel892f1672016-04-11 20:13:44 +00005398 for (BasicBlock &BB : F) {
Tim Northovercea0abb2014-03-29 08:22:29 +00005399 // Does this BB end with the following?
5400 // %andVal = and %val, #single-bit-set
5401 // %icmpVal = icmp %andResult, 0
5402 // br i1 %cmpVal label %dest1, label %dest2"
Sanjay Patel892f1672016-04-11 20:13:44 +00005403 BranchInst *Brcc = dyn_cast<BranchInst>(BB.getTerminator());
Tim Northovercea0abb2014-03-29 08:22:29 +00005404 if (!Brcc || !Brcc->isConditional())
5405 continue;
5406 ICmpInst *Cmp = dyn_cast<ICmpInst>(Brcc->getOperand(0));
Sanjay Patel892f1672016-04-11 20:13:44 +00005407 if (!Cmp || Cmp->getParent() != &BB)
Tim Northovercea0abb2014-03-29 08:22:29 +00005408 continue;
5409 ConstantInt *Zero = dyn_cast<ConstantInt>(Cmp->getOperand(1));
5410 if (!Zero || !Zero->isZero())
5411 continue;
5412 Instruction *And = dyn_cast<Instruction>(Cmp->getOperand(0));
Sanjay Patel892f1672016-04-11 20:13:44 +00005413 if (!And || And->getOpcode() != Instruction::And || And->getParent() != &BB)
Tim Northovercea0abb2014-03-29 08:22:29 +00005414 continue;
5415 ConstantInt* Mask = dyn_cast<ConstantInt>(And->getOperand(1));
5416 if (!Mask || !Mask->getUniqueInteger().isPowerOf2())
5417 continue;
Sanjay Patel892f1672016-04-11 20:13:44 +00005418 DEBUG(dbgs() << "found and; icmp ?,0; brcc\n"); DEBUG(BB.dump());
Tim Northovercea0abb2014-03-29 08:22:29 +00005419
5420 // Push the "and; icmp" for any users that are conditional branches.
5421 // Since there can only be one branch use per BB, we don't need to keep
5422 // track of which BBs we insert into.
Sanjay Patel892f1672016-04-11 20:13:44 +00005423 for (Use &TheUse : Cmp->uses()) {
Tim Northovercea0abb2014-03-29 08:22:29 +00005424 // Find brcc use.
Sanjay Patel892f1672016-04-11 20:13:44 +00005425 BranchInst *BrccUser = dyn_cast<BranchInst>(TheUse);
Tim Northovercea0abb2014-03-29 08:22:29 +00005426 if (!BrccUser || !BrccUser->isConditional())
5427 continue;
5428 BasicBlock *UserBB = BrccUser->getParent();
Sanjay Patel892f1672016-04-11 20:13:44 +00005429 if (UserBB == &BB) continue;
Tim Northovercea0abb2014-03-29 08:22:29 +00005430 DEBUG(dbgs() << "found Brcc use\n");
5431
5432 // Sink the "and; icmp" to use.
5433 MadeChange = true;
5434 BinaryOperator *NewAnd =
5435 BinaryOperator::CreateAnd(And->getOperand(0), And->getOperand(1), "",
5436 BrccUser);
5437 CmpInst *NewCmp =
5438 CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(), NewAnd, Zero,
5439 "", BrccUser);
5440 TheUse = NewCmp;
5441 ++NumAndCmpsMoved;
5442 DEBUG(BrccUser->getParent()->dump());
5443 }
5444 }
5445 return MadeChange;
5446}
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005447
5448/// \brief Scale down both weights to fit into uint32_t.
5449static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) {
5450 uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
5451 uint32_t Scale = (NewMax / UINT32_MAX) + 1;
5452 NewTrue = NewTrue / Scale;
5453 NewFalse = NewFalse / Scale;
5454}
5455
5456/// \brief Some targets prefer to split a conditional branch like:
5457/// \code
5458/// %0 = icmp ne i32 %a, 0
5459/// %1 = icmp ne i32 %b, 0
5460/// %or.cond = or i1 %0, %1
5461/// br i1 %or.cond, label %TrueBB, label %FalseBB
5462/// \endcode
5463/// into multiple branch instructions like:
5464/// \code
5465/// bb1:
5466/// %0 = icmp ne i32 %a, 0
5467/// br i1 %0, label %TrueBB, label %bb2
5468/// bb2:
5469/// %1 = icmp ne i32 %b, 0
5470/// br i1 %1, label %TrueBB, label %FalseBB
5471/// \endcode
5472/// This usually allows instruction selection to do even further optimizations
5473/// and combine the compare with the branch instruction. Currently this is
5474/// applied for targets which have "cheap" jump instructions.
5475///
5476/// FIXME: Remove the (equivalent?) implementation in SelectionDAG.
5477///
5478bool CodeGenPrepare::splitBranchCondition(Function &F) {
David Blaikiedc3f01e2015-03-09 01:57:13 +00005479 if (!TM || !TM->Options.EnableFastISel || !TLI || TLI->isJumpExpensive())
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005480 return false;
5481
5482 bool MadeChange = false;
5483 for (auto &BB : F) {
5484 // Does this BB end with the following?
5485 // %cond1 = icmp|fcmp|binary instruction ...
5486 // %cond2 = icmp|fcmp|binary instruction ...
5487 // %cond.or = or|and i1 %cond1, cond2
5488 // br i1 %cond.or label %dest1, label %dest2"
5489 BinaryOperator *LogicOp;
5490 BasicBlock *TBB, *FBB;
5491 if (!match(BB.getTerminator(), m_Br(m_OneUse(m_BinOp(LogicOp)), TBB, FBB)))
5492 continue;
5493
Sanjay Patel42574202015-09-02 19:23:23 +00005494 auto *Br1 = cast<BranchInst>(BB.getTerminator());
5495 if (Br1->getMetadata(LLVMContext::MD_unpredictable))
5496 continue;
5497
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005498 unsigned Opc;
Juergen Ributzka8bda7382014-12-09 17:50:10 +00005499 Value *Cond1, *Cond2;
5500 if (match(LogicOp, m_And(m_OneUse(m_Value(Cond1)),
5501 m_OneUse(m_Value(Cond2)))))
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005502 Opc = Instruction::And;
Juergen Ributzka8bda7382014-12-09 17:50:10 +00005503 else if (match(LogicOp, m_Or(m_OneUse(m_Value(Cond1)),
5504 m_OneUse(m_Value(Cond2)))))
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005505 Opc = Instruction::Or;
5506 else
5507 continue;
5508
5509 if (!match(Cond1, m_CombineOr(m_Cmp(), m_BinOp())) ||
5510 !match(Cond2, m_CombineOr(m_Cmp(), m_BinOp())) )
5511 continue;
5512
5513 DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump());
5514
5515 // Create a new BB.
Duncan P. N. Exon Smitha848c472016-02-21 19:52:15 +00005516 auto TmpBB =
5517 BasicBlock::Create(BB.getContext(), BB.getName() + ".cond.split",
5518 BB.getParent(), BB.getNextNode());
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005519
5520 // Update original basic block by using the first condition directly by the
5521 // branch instruction and removing the no longer needed and/or instruction.
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005522 Br1->setCondition(Cond1);
5523 LogicOp->eraseFromParent();
Juergen Ributzka8bda7382014-12-09 17:50:10 +00005524
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005525 // Depending on the conditon we have to either replace the true or the false
5526 // successor of the original branch instruction.
5527 if (Opc == Instruction::And)
5528 Br1->setSuccessor(0, TmpBB);
5529 else
5530 Br1->setSuccessor(1, TmpBB);
5531
5532 // Fill in the new basic block.
5533 auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB);
Juergen Ributzka8bda7382014-12-09 17:50:10 +00005534 if (auto *I = dyn_cast<Instruction>(Cond2)) {
5535 I->removeFromParent();
5536 I->insertBefore(Br2);
5537 }
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005538
5539 // Update PHI nodes in both successors. The original BB needs to be
5540 // replaced in one succesor's PHI nodes, because the branch comes now from
5541 // the newly generated BB (NewBB). In the other successor we need to add one
5542 // incoming edge to the PHI nodes, because both branch instructions target
5543 // now the same successor. Depending on the original branch condition
5544 // (and/or) we have to swap the successors (TrueDest, FalseDest), so that
5545 // we perfrom the correct update for the PHI nodes.
5546 // This doesn't change the successor order of the just created branch
5547 // instruction (or any other instruction).
5548 if (Opc == Instruction::Or)
5549 std::swap(TBB, FBB);
5550
5551 // Replace the old BB with the new BB.
5552 for (auto &I : *TBB) {
5553 PHINode *PN = dyn_cast<PHINode>(&I);
5554 if (!PN)
5555 break;
5556 int i;
5557 while ((i = PN->getBasicBlockIndex(&BB)) >= 0)
5558 PN->setIncomingBlock(i, TmpBB);
5559 }
5560
5561 // Add another incoming edge form the new BB.
5562 for (auto &I : *FBB) {
5563 PHINode *PN = dyn_cast<PHINode>(&I);
5564 if (!PN)
5565 break;
5566 auto *Val = PN->getIncomingValueForBlock(&BB);
5567 PN->addIncoming(Val, TmpBB);
5568 }
5569
5570 // Update the branch weights (from SelectionDAGBuilder::
5571 // FindMergedConditions).
5572 if (Opc == Instruction::Or) {
5573 // Codegen X | Y as:
5574 // BB1:
5575 // jmp_if_X TBB
5576 // jmp TmpBB
5577 // TmpBB:
5578 // jmp_if_Y TBB
5579 // jmp FBB
5580 //
5581
5582 // We have flexibility in setting Prob for BB1 and Prob for NewBB.
5583 // The requirement is that
5584 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
5585 // = TrueProb for orignal BB.
5586 // Assuming the orignal weights are A and B, one choice is to set BB1's
5587 // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice
5588 // assumes that
5589 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
5590 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
5591 // TmpBB, but the math is more complicated.
5592 uint64_t TrueWeight, FalseWeight;
Sanjay Pateldc88bd62016-04-23 20:01:22 +00005593 if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005594 uint64_t NewTrueWeight = TrueWeight;
5595 uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
5596 scaleWeights(NewTrueWeight, NewFalseWeight);
5597 Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
5598 .createBranchWeights(TrueWeight, FalseWeight));
5599
5600 NewTrueWeight = TrueWeight;
5601 NewFalseWeight = 2 * FalseWeight;
5602 scaleWeights(NewTrueWeight, NewFalseWeight);
5603 Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
5604 .createBranchWeights(TrueWeight, FalseWeight));
5605 }
5606 } else {
5607 // Codegen X & Y as:
5608 // BB1:
5609 // jmp_if_X TmpBB
5610 // jmp FBB
5611 // TmpBB:
5612 // jmp_if_Y TBB
5613 // jmp FBB
5614 //
5615 // This requires creation of TmpBB after CurBB.
5616
5617 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
5618 // The requirement is that
5619 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
5620 // = FalseProb for orignal BB.
5621 // Assuming the orignal weights are A and B, one choice is to set BB1's
5622 // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice
5623 // assumes that
5624 // FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB.
5625 uint64_t TrueWeight, FalseWeight;
Sanjay Pateldc88bd62016-04-23 20:01:22 +00005626 if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005627 uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
5628 uint64_t NewFalseWeight = FalseWeight;
5629 scaleWeights(NewTrueWeight, NewFalseWeight);
5630 Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
5631 .createBranchWeights(TrueWeight, FalseWeight));
5632
5633 NewTrueWeight = 2 * TrueWeight;
5634 NewFalseWeight = FalseWeight;
5635 scaleWeights(NewTrueWeight, NewFalseWeight);
5636 Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
5637 .createBranchWeights(TrueWeight, FalseWeight));
5638 }
5639 }
5640
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005641 // Note: No point in getting fancy here, since the DT info is never
Quentin Colombet7bdd50d2015-03-18 23:17:28 +00005642 // available to CodeGenPrepare.
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00005643 ModifiedDT = true;
5644
5645 MadeChange = true;
5646
5647 DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump();
5648 TmpBB->dump());
5649 }
5650 return MadeChange;
5651}
Piotr Padlewski6c15ec42015-09-15 18:32:14 +00005652
5653void CodeGenPrepare::stripInvariantGroupMetadata(Instruction &I) {
Piotr Padlewskiea092882015-09-17 20:25:07 +00005654 if (auto *InvariantMD = I.getMetadata(LLVMContext::MD_invariant_group))
Piotr Padlewski6c15ec42015-09-15 18:32:14 +00005655 I.dropUnknownNonDebugMetadata(InvariantMD->getMetadataID());
5656}