blob: 13917f469ea78e341bb4ca0101914e36620727bc [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"
Chandler Carruth62d42152015-01-15 02:16:27 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
Quentin Colombetc32615d2014-10-31 17:52:53 +000022#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000023#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000027#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Function.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000029#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/IRBuilder.h"
31#include "llvm/IR/InlineAsm.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +000034#include "llvm/IR/MDBuilder.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000035#include "llvm/IR/PatternMatch.h"
Ramkumar Ramachandradba73292015-01-14 23:27:07 +000036#include "llvm/IR/Statepoint.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000037#include "llvm/IR/ValueHandle.h"
Chandler Carrutha4ea2692014-03-04 11:26:31 +000038#include "llvm/IR/ValueMap.h"
Chris Lattnerf2836d12007-03-31 04:06:36 +000039#include "llvm/Pass.h"
Evan Cheng8b637b12010-08-17 01:34:49 +000040#include "llvm/Support/CommandLine.h"
Evan Chengd3d80172007-12-05 23:58:20 +000041#include "llvm/Support/Debug.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000042#include "llvm/Support/raw_ostream.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000043#include "llvm/Target/TargetLowering.h"
Hal Finkelc3998302014-04-12 00:59:48 +000044#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000045#include "llvm/Transforms/Utils/BasicBlockUtils.h"
46#include "llvm/Transforms/Utils/BuildLibCalls.h"
Preston Gurdcdf540d2012-09-04 18:22:17 +000047#include "llvm/Transforms/Utils/BypassSlowDivision.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000048#include "llvm/Transforms/Utils/Local.h"
Ahmed Bougachae03bef72015-01-12 17:22:43 +000049#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Chris Lattnerf2836d12007-03-31 04:06:36 +000050using namespace llvm;
Chris Lattnerd616ef52008-11-25 04:42:10 +000051using namespace llvm::PatternMatch;
Chris Lattnerf2836d12007-03-31 04:06:36 +000052
Chandler Carruth1b9dde02014-04-22 02:02:50 +000053#define DEBUG_TYPE "codegenprepare"
54
Cameron Zwarichced753f2011-01-05 17:27:27 +000055STATISTIC(NumBlocksElim, "Number of blocks eliminated");
Evan Cheng0663f232011-03-21 01:19:09 +000056STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
57STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
Cameron Zwarichced753f2011-01-05 17:27:27 +000058STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
59 "sunken Cmps");
60STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
61 "of sunken Casts");
62STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
63 "computations were sunk");
Evan Cheng0663f232011-03-21 01:19:09 +000064STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
65STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
66STATISTIC(NumRetsDup, "Number of return instructions duplicated");
Devang Patel53771ba2011-08-18 00:50:51 +000067STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved");
Benjamin Kramer047d7ca2012-05-05 12:49:22 +000068STATISTIC(NumSelectsExpanded, "Number of selects turned into branches");
Tim Northovercea0abb2014-03-29 08:22:29 +000069STATISTIC(NumAndCmpsMoved, "Number of and/cmp's pushed into branches");
Quentin Colombetc32615d2014-10-31 17:52:53 +000070STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed");
Jakob Stoklund Oleseneb12f492010-09-30 20:51:52 +000071
Cameron Zwarich338d3622011-03-11 21:52:04 +000072static cl::opt<bool> DisableBranchOpts(
73 "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
74 cl::desc("Disable branch optimizations in CodeGenPrepare"));
75
Ramkumar Ramachandradba73292015-01-14 23:27:07 +000076static cl::opt<bool>
77 DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false),
78 cl::desc("Disable GC optimizations in CodeGenPrepare"));
79
Benjamin Kramer3d38c172012-05-06 14:25:16 +000080static cl::opt<bool> DisableSelectToBranch(
81 "disable-cgp-select2branch", cl::Hidden, cl::init(false),
82 cl::desc("Disable select to branch conversion."));
Benjamin Kramer047d7ca2012-05-05 12:49:22 +000083
Hal Finkelc3998302014-04-12 00:59:48 +000084static cl::opt<bool> AddrSinkUsingGEPs(
85 "addr-sink-using-gep", cl::Hidden, cl::init(false),
86 cl::desc("Address sinking in CGP using GEPs."));
87
Tim Northovercea0abb2014-03-29 08:22:29 +000088static cl::opt<bool> EnableAndCmpSinking(
89 "enable-andcmp-sinking", cl::Hidden, cl::init(true),
90 cl::desc("Enable sinkinig and/cmp into branches."));
91
Quentin Colombetc32615d2014-10-31 17:52:53 +000092static cl::opt<bool> DisableStoreExtract(
93 "disable-cgp-store-extract", cl::Hidden, cl::init(false),
94 cl::desc("Disable store(extract) optimizations in CodeGenPrepare"));
95
96static cl::opt<bool> StressStoreExtract(
97 "stress-cgp-store-extract", cl::Hidden, cl::init(false),
98 cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"));
99
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000100static cl::opt<bool> DisableExtLdPromotion(
101 "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
102 cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in "
103 "CodeGenPrepare"));
104
105static cl::opt<bool> StressExtLdPromotion(
106 "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
107 cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) "
108 "optimization in CodeGenPrepare"));
109
Eric Christopherc1ea1492008-09-24 05:32:41 +0000110namespace {
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000111typedef SmallPtrSet<Instruction *, 16> SetOfInstrs;
Quentin Colombetf5485bb2014-11-13 01:44:51 +0000112struct TypeIsSExt {
113 Type *Ty;
114 bool IsSExt;
115 TypeIsSExt(Type *Ty, bool IsSExt) : Ty(Ty), IsSExt(IsSExt) {}
116};
117typedef DenseMap<Instruction *, TypeIsSExt> InstrToOrigTy;
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000118class TypePromotionTransaction;
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000119
Chris Lattner2dd09db2009-09-02 06:11:42 +0000120 class CodeGenPrepare : public FunctionPass {
Chris Lattnerf2836d12007-03-31 04:06:36 +0000121 /// TLI - Keep a pointer of a TargetLowering to consult for determining
122 /// transformation profitability.
Bill Wendling7a639ea2013-06-19 21:07:11 +0000123 const TargetMachine *TM;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000124 const TargetLowering *TLI;
Quentin Colombetc32615d2014-10-31 17:52:53 +0000125 const TargetTransformInfo *TTI;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000126 const TargetLibraryInfo *TLInfo;
Nadav Rotem465834c2012-07-24 10:51:42 +0000127
Chris Lattner7a277142011-01-15 07:14:54 +0000128 /// CurInstIterator - As we scan instructions optimizing them, this is the
129 /// next instruction to optimize. Xforms that can invalidate this should
130 /// update it.
131 BasicBlock::iterator CurInstIterator;
Evan Cheng3b3de7c2008-12-19 18:03:11 +0000132
Evan Cheng0663f232011-03-21 01:19:09 +0000133 /// Keeps track of non-local addresses that have been sunk into a block.
134 /// This allows us to avoid inserting duplicate code for blocks with
135 /// multiple load/stores of the same address.
Nick Lewycky5fb19632013-05-08 09:00:10 +0000136 ValueMap<Value*, Value*> SunkAddrs;
Cameron Zwarichce3b9302011-01-06 00:42:50 +0000137
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000138 /// Keeps track of all truncates inserted for the current function.
139 SetOfInstrs InsertedTruncsSet;
140 /// Keeps track of the type of the related instruction before their
141 /// promotion for the current function.
142 InstrToOrigTy PromotedInsts;
143
Quentin Colombet7bdd50d2015-03-18 23:17:28 +0000144 /// ModifiedDT - If CFG is modified in anyway.
Devang Patel8f606d72011-03-24 15:35:25 +0000145 bool ModifiedDT;
Evan Cheng0663f232011-03-21 01:19:09 +0000146
Benjamin Kramer047d7ca2012-05-05 12:49:22 +0000147 /// OptSize - True if optimizing for size.
148 bool OptSize;
149
Chris Lattnerf2836d12007-03-31 04:06:36 +0000150 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000151 static char ID; // Pass identification, replacement for typeid
Craig Topperc0196b12014-04-14 00:51:57 +0000152 explicit CodeGenPrepare(const TargetMachine *TM = nullptr)
Quentin Colombetc32615d2014-10-31 17:52:53 +0000153 : FunctionPass(ID), TM(TM), TLI(nullptr), TTI(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000154 initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
155 }
Craig Topper4584cd52014-03-07 09:26:03 +0000156 bool runOnFunction(Function &F) override;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000157
Craig Topper4584cd52014-03-07 09:26:03 +0000158 const char *getPassName() const override { return "CodeGen Prepare"; }
Evan Cheng99cafb12012-12-21 01:48:14 +0000159
Craig Topper4584cd52014-03-07 09:26:03 +0000160 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth73523022014-01-13 13:07:17 +0000161 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000162 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth705b1852015-01-31 03:43:40 +0000163 AU.addRequired<TargetTransformInfoWrapperPass>();
Andreas Neustifterf8cb7582009-09-16 09:26:52 +0000164 }
165
Chris Lattnerf2836d12007-03-31 04:06:36 +0000166 private:
Nadav Rotem70409992012-08-14 05:19:07 +0000167 bool EliminateFallThrough(Function &F);
Chris Lattnerc3748562007-04-02 01:35:34 +0000168 bool EliminateMostlyEmptyBlocks(Function &F);
169 bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
170 void EliminateMostlyEmptyBlock(BasicBlock *BB);
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000171 bool OptimizeBlock(BasicBlock &BB, bool& ModifiedDT);
172 bool OptimizeInst(Instruction *I, bool& ModifiedDT);
Chris Lattner229907c2011-07-18 04:54:35 +0000173 bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy);
Chris Lattner7a277142011-01-15 07:14:54 +0000174 bool OptimizeInlineAsmInst(CallInst *CS);
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000175 bool OptimizeCallInst(CallInst *CI, bool& ModifiedDT);
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000176 bool MoveExtToFormExtLoad(Instruction *&I);
Evan Chengd3d80172007-12-05 23:58:20 +0000177 bool OptimizeExtUses(Instruction *I);
Benjamin Kramer047d7ca2012-05-05 12:49:22 +0000178 bool OptimizeSelectInst(SelectInst *SI);
Tim Northoveraeb8e062014-02-19 10:02:43 +0000179 bool OptimizeShuffleVectorInst(ShuffleVectorInst *SI);
Quentin Colombetc32615d2014-10-31 17:52:53 +0000180 bool OptimizeExtractElementInst(Instruction *Inst);
Benjamin Kramer455fa352012-11-23 19:17:06 +0000181 bool DupRetToEnableTailCallOpts(BasicBlock *BB);
Devang Patel53771ba2011-08-18 00:50:51 +0000182 bool PlaceDbgValues(Function &F);
Tim Northovercea0abb2014-03-29 08:22:29 +0000183 bool sinkAndCmp(Function &F);
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000184 bool ExtLdPromotion(TypePromotionTransaction &TPT, LoadInst *&LI,
185 Instruction *&Inst,
186 const SmallVectorImpl<Instruction *> &Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +0000187 unsigned CreatedInstCost);
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000188 bool splitBranchCondition(Function &F);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000189 bool simplifyOffsetableRelocate(Instruction &I);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000190 };
191}
Devang Patel09f162c2007-05-01 21:15:47 +0000192
Devang Patel8c78a0b2007-05-03 01:11:54 +0000193char CodeGenPrepare::ID = 0;
Jiangning Liud623c522014-06-11 07:04:37 +0000194INITIALIZE_TM_PASS(CodeGenPrepare, "codegenprepare",
195 "Optimize for code generation", false, false)
Chris Lattnerf2836d12007-03-31 04:06:36 +0000196
Bill Wendling7a639ea2013-06-19 21:07:11 +0000197FunctionPass *llvm::createCodeGenPreparePass(const TargetMachine *TM) {
198 return new CodeGenPrepare(TM);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000199}
200
Chris Lattnerf2836d12007-03-31 04:06:36 +0000201bool CodeGenPrepare::runOnFunction(Function &F) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000202 if (skipOptnoneFunction(F))
203 return false;
204
Chris Lattnerf2836d12007-03-31 04:06:36 +0000205 bool EverMadeChange = false;
Quentin Colombet3a4bf042014-02-06 21:44:56 +0000206 // Clear per function information.
207 InsertedTruncsSet.clear();
208 PromotedInsts.clear();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000209
Devang Patel8f606d72011-03-24 15:35:25 +0000210 ModifiedDT = false;
Eric Christopherd9134482014-08-04 21:25:23 +0000211 if (TM)
Eric Christopherfccff372015-01-27 01:01:38 +0000212 TLI = TM->getSubtargetImpl(F)->getTargetLowering();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000213 TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthfdb9c572015-02-01 12:01:35 +0000214 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Duncan P. N. Exon Smith70eb9c52015-02-14 01:44:41 +0000215 OptSize = F.hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng0663f232011-03-21 01:19:09 +0000216
Preston Gurdcdf540d2012-09-04 18:22:17 +0000217 /// This optimization identifies DIV instructions that can be
218 /// profitably bypassed and carried out with a shorter, faster divide.
Preston Gurd485296d2013-03-04 18:13:57 +0000219 if (!OptSize && TLI && TLI->isSlowDivBypassed()) {
Preston Gurd0d67f512012-10-04 21:33:40 +0000220 const DenseMap<unsigned int, unsigned int> &BypassWidths =
221 TLI->getBypassSlowDivWidths();
Evan Cheng71be12b2012-09-14 21:25:34 +0000222 for (Function::iterator I = F.begin(); I != F.end(); I++)
Preston Gurd0d67f512012-10-04 21:33:40 +0000223 EverMadeChange |= bypassSlowDivision(F, I, BypassWidths);
Preston Gurdcdf540d2012-09-04 18:22:17 +0000224 }
225
226 // Eliminate blocks that contain only PHI nodes and an
Chris Lattnerc3748562007-04-02 01:35:34 +0000227 // unconditional branch.
228 EverMadeChange |= EliminateMostlyEmptyBlocks(F);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000229
Devang Patel53771ba2011-08-18 00:50:51 +0000230 // llvm.dbg.value is far away from the value then iSel may not be able
Nadav Rotem465834c2012-07-24 10:51:42 +0000231 // handle it properly. iSel will drop llvm.dbg.value if it can not
Devang Patel53771ba2011-08-18 00:50:51 +0000232 // find a node corresponding to the value.
233 EverMadeChange |= PlaceDbgValues(F);
234
Tim Northovercea0abb2014-03-29 08:22:29 +0000235 // If there is a mask, compare against zero, and branch that can be combined
236 // into a single target instruction, push the mask and compare into branch
237 // users. Do this before OptimizeBlock -> OptimizeInst ->
238 // OptimizeCmpExpression, which perturbs the pattern being searched for.
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000239 if (!DisableBranchOpts) {
Tim Northovercea0abb2014-03-29 08:22:29 +0000240 EverMadeChange |= sinkAndCmp(F);
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +0000241 EverMadeChange |= splitBranchCondition(F);
242 }
Tim Northovercea0abb2014-03-29 08:22:29 +0000243
Chris Lattnerc3748562007-04-02 01:35:34 +0000244 bool MadeChange = true;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000245 while (MadeChange) {
246 MadeChange = false;
Hans Wennborg02fbc712012-09-19 07:48:16 +0000247 for (Function::iterator I = F.begin(); I != F.end(); ) {
Evan Cheng0663f232011-03-21 01:19:09 +0000248 BasicBlock *BB = I++;
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000249 bool ModifiedDTOnIteration = false;
250 MadeChange |= OptimizeBlock(*BB, ModifiedDTOnIteration);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000251
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000252 // Restart BB iteration if the dominator tree of the Function was changed
Elena Demikhovsky87700a72014-12-28 08:54:45 +0000253 if (ModifiedDTOnIteration)
254 break;
Evan Cheng0663f232011-03-21 01:19:09 +0000255 }
Chris Lattnerf2836d12007-03-31 04:06:36 +0000256 EverMadeChange |= MadeChange;
257 }
Cameron Zwarichce3b9302011-01-06 00:42:50 +0000258
259 SunkAddrs.clear();
260
Cameron Zwarich338d3622011-03-11 21:52:04 +0000261 if (!DisableBranchOpts) {
262 MadeChange = false;
Bill Wendling97b93592012-03-04 10:46:01 +0000263 SmallPtrSet<BasicBlock*, 8> WorkList;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +0000264 for (BasicBlock &BB : F) {
265 SmallVector<BasicBlock *, 2> Successors(succ_begin(&BB), succ_end(&BB));
266 MadeChange |= ConstantFoldTerminator(&BB, true);
Bill Wendling97b93592012-03-04 10:46:01 +0000267 if (!MadeChange) continue;
268
269 for (SmallVectorImpl<BasicBlock*>::iterator
270 II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
271 if (pred_begin(*II) == pred_end(*II))
272 WorkList.insert(*II);
273 }
274
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000275 // Delete the dead blocks and any of their dead successors.
Bill Wendlingab417b62012-12-06 00:30:20 +0000276 MadeChange |= !WorkList.empty();
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000277 while (!WorkList.empty()) {
278 BasicBlock *BB = *WorkList.begin();
279 WorkList.erase(BB);
280 SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB));
281
282 DeleteDeadBlock(BB);
Stephen Lin837bba12013-07-15 17:55:02 +0000283
Bill Wendlingf3614fd2012-11-28 23:23:48 +0000284 for (SmallVectorImpl<BasicBlock*>::iterator
285 II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
286 if (pred_begin(*II) == pred_end(*II))
287 WorkList.insert(*II);
288 }
Cameron Zwarich338d3622011-03-11 21:52:04 +0000289
Nadav Rotem70409992012-08-14 05:19:07 +0000290 // Merge pairs of basic blocks with unconditional branches, connected by
291 // a single edge.
292 if (EverMadeChange || MadeChange)
293 MadeChange |= EliminateFallThrough(F);
294
Cameron Zwarich338d3622011-03-11 21:52:04 +0000295 EverMadeChange |= MadeChange;
296 }
297
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000298 if (!DisableGCOpts) {
299 SmallVector<Instruction *, 2> Statepoints;
300 for (BasicBlock &BB : F)
301 for (Instruction &I : BB)
302 if (isStatepoint(I))
303 Statepoints.push_back(&I);
304 for (auto &I : Statepoints)
305 EverMadeChange |= simplifyOffsetableRelocate(*I);
306 }
307
Chris Lattnerf2836d12007-03-31 04:06:36 +0000308 return EverMadeChange;
309}
310
Nadav Rotem70409992012-08-14 05:19:07 +0000311/// EliminateFallThrough - Merge basic blocks which are connected
312/// by a single edge, where one of the basic blocks has a single successor
313/// pointing to the other basic block, which has a single predecessor.
314bool CodeGenPrepare::EliminateFallThrough(Function &F) {
315 bool Changed = false;
316 // Scan all of the blocks in the function, except for the entry block.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000317 for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
Nadav Rotem70409992012-08-14 05:19:07 +0000318 BasicBlock *BB = I++;
319 // If the destination block has a single pred, then this is a trivial
320 // edge, just collapse it.
321 BasicBlock *SinglePred = BB->getSinglePredecessor();
322
Evan Cheng64a223a2012-09-28 23:58:57 +0000323 // Don't merge if BB's address is taken.
324 if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue;
Nadav Rotem70409992012-08-14 05:19:07 +0000325
326 BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
327 if (Term && !Term->isConditional()) {
328 Changed = true;
Michael Liao6e12d122012-08-21 05:55:22 +0000329 DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n");
Nadav Rotem70409992012-08-14 05:19:07 +0000330 // Remember if SinglePred was the entry block of the function.
331 // If so, we will need to move BB back to the entry position.
332 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
Quentin Colombet7bdd50d2015-03-18 23:17:28 +0000333 MergeBasicBlockIntoOnlyPred(BB, nullptr);
Nadav Rotem70409992012-08-14 05:19:07 +0000334
335 if (isEntry && BB != &BB->getParent()->getEntryBlock())
336 BB->moveBefore(&BB->getParent()->getEntryBlock());
337
338 // We have erased a block. Update the iterator.
339 I = BB;
Nadav Rotem70409992012-08-14 05:19:07 +0000340 }
341 }
342 return Changed;
343}
344
Dale Johannesen4026b042009-03-27 01:13:37 +0000345/// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
346/// debug info directives, and an unconditional branch. Passes before isel
347/// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
348/// isel. Start by eliminating these blocks so we can split them the way we
349/// want them.
Chris Lattnerc3748562007-04-02 01:35:34 +0000350bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
351 bool MadeChange = false;
352 // Note that this intentionally skips the entry block.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000353 for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
Chris Lattnerc3748562007-04-02 01:35:34 +0000354 BasicBlock *BB = I++;
355
356 // If this block doesn't end with an uncond branch, ignore it.
357 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
358 if (!BI || !BI->isUnconditional())
359 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000360
Dale Johannesen4026b042009-03-27 01:13:37 +0000361 // If the instruction before the branch (skipping debug info) isn't a phi
362 // node, then other stuff is happening here.
Chris Lattnerc3748562007-04-02 01:35:34 +0000363 BasicBlock::iterator BBI = BI;
364 if (BBI != BB->begin()) {
365 --BBI;
Dale Johannesen4026b042009-03-27 01:13:37 +0000366 while (isa<DbgInfoIntrinsic>(BBI)) {
367 if (BBI == BB->begin())
368 break;
369 --BBI;
370 }
371 if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
372 continue;
Chris Lattnerc3748562007-04-02 01:35:34 +0000373 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000374
Chris Lattnerc3748562007-04-02 01:35:34 +0000375 // Do not break infinite loops.
376 BasicBlock *DestBB = BI->getSuccessor(0);
377 if (DestBB == BB)
378 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000379
Chris Lattnerc3748562007-04-02 01:35:34 +0000380 if (!CanMergeBlocks(BB, DestBB))
381 continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000382
Chris Lattnerc3748562007-04-02 01:35:34 +0000383 EliminateMostlyEmptyBlock(BB);
384 MadeChange = true;
385 }
386 return MadeChange;
387}
388
389/// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
390/// single uncond branch between them, and BB contains no other non-phi
391/// instructions.
392bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
393 const BasicBlock *DestBB) const {
394 // We only want to eliminate blocks whose phi nodes are used by phi nodes in
395 // the successor. If there are more complex condition (e.g. preheaders),
396 // don't mess around with them.
397 BasicBlock::const_iterator BBI = BB->begin();
398 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000399 for (const User *U : PN->users()) {
400 const Instruction *UI = cast<Instruction>(U);
401 if (UI->getParent() != DestBB || !isa<PHINode>(UI))
Chris Lattnerc3748562007-04-02 01:35:34 +0000402 return false;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000403 // If User is inside DestBB block and it is a PHINode then check
404 // incoming value. If incoming value is not from BB then this is
Devang Pateld3208522007-04-25 00:37:04 +0000405 // a complex condition (e.g. preheaders) we want to avoid here.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000406 if (UI->getParent() == DestBB) {
407 if (const PHINode *UPN = dyn_cast<PHINode>(UI))
Devang Pateld3208522007-04-25 00:37:04 +0000408 for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
409 Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
410 if (Insn && Insn->getParent() == BB &&
411 Insn->getParent() != UPN->getIncomingBlock(I))
412 return false;
413 }
414 }
Chris Lattnerc3748562007-04-02 01:35:34 +0000415 }
416 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000417
Chris Lattnerc3748562007-04-02 01:35:34 +0000418 // If BB and DestBB contain any common predecessors, then the phi nodes in BB
419 // and DestBB may have conflicting incoming values for the block. If so, we
420 // can't merge the block.
421 const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
422 if (!DestBBPN) return true; // no conflict.
Eric Christopherc1ea1492008-09-24 05:32:41 +0000423
Chris Lattnerc3748562007-04-02 01:35:34 +0000424 // Collect the preds of BB.
Chris Lattner8201a9b2007-11-06 22:07:40 +0000425 SmallPtrSet<const BasicBlock*, 16> BBPreds;
Chris Lattnerc3748562007-04-02 01:35:34 +0000426 if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
427 // It is faster to get preds from a PHI than with pred_iterator.
428 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
429 BBPreds.insert(BBPN->getIncomingBlock(i));
430 } else {
431 BBPreds.insert(pred_begin(BB), pred_end(BB));
432 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000433
Chris Lattnerc3748562007-04-02 01:35:34 +0000434 // Walk the preds of DestBB.
435 for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
436 BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
437 if (BBPreds.count(Pred)) { // Common predecessor?
438 BBI = DestBB->begin();
439 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
440 const Value *V1 = PN->getIncomingValueForBlock(Pred);
441 const Value *V2 = PN->getIncomingValueForBlock(BB);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000442
Chris Lattnerc3748562007-04-02 01:35:34 +0000443 // If V2 is a phi node in BB, look up what the mapped value will be.
444 if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
445 if (V2PN->getParent() == BB)
446 V2 = V2PN->getIncomingValueForBlock(Pred);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000447
Chris Lattnerc3748562007-04-02 01:35:34 +0000448 // If there is a conflict, bail out.
449 if (V1 != V2) return false;
450 }
451 }
452 }
453
454 return true;
455}
456
457
458/// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
459/// an unconditional branch in it.
460void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
461 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
462 BasicBlock *DestBB = BI->getSuccessor(0);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000463
David Greene74e2d492010-01-05 01:27:11 +0000464 DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000465
Chris Lattnerc3748562007-04-02 01:35:34 +0000466 // If the destination block has a single pred, then this is a trivial edge,
467 // just collapse it.
Chris Lattner4059f432008-11-27 19:29:14 +0000468 if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
Chris Lattner8a172da2008-11-28 19:54:49 +0000469 if (SinglePred != DestBB) {
470 // Remember if SinglePred was the entry block of the function. If so, we
471 // will need to move BB back to the entry position.
472 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
Quentin Colombet7bdd50d2015-03-18 23:17:28 +0000473 MergeBasicBlockIntoOnlyPred(DestBB, nullptr);
Chris Lattner4059f432008-11-27 19:29:14 +0000474
Chris Lattner8a172da2008-11-28 19:54:49 +0000475 if (isEntry && BB != &BB->getParent()->getEntryBlock())
476 BB->moveBefore(&BB->getParent()->getEntryBlock());
Nadav Rotem465834c2012-07-24 10:51:42 +0000477
David Greene74e2d492010-01-05 01:27:11 +0000478 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattner8a172da2008-11-28 19:54:49 +0000479 return;
480 }
Chris Lattnerc3748562007-04-02 01:35:34 +0000481 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000482
Chris Lattnerc3748562007-04-02 01:35:34 +0000483 // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB
484 // to handle the new incoming edges it is about to have.
485 PHINode *PN;
486 for (BasicBlock::iterator BBI = DestBB->begin();
487 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
488 // Remove the incoming value for BB, and remember it.
489 Value *InVal = PN->removeIncomingValue(BB, false);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000490
Chris Lattnerc3748562007-04-02 01:35:34 +0000491 // Two options: either the InVal is a phi node defined in BB or it is some
492 // value that dominates BB.
493 PHINode *InValPhi = dyn_cast<PHINode>(InVal);
494 if (InValPhi && InValPhi->getParent() == BB) {
495 // Add all of the input values of the input PHI as inputs of this phi.
496 for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
497 PN->addIncoming(InValPhi->getIncomingValue(i),
498 InValPhi->getIncomingBlock(i));
499 } else {
500 // Otherwise, add one instance of the dominating value for each edge that
501 // we will be adding.
502 if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
503 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
504 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
505 } else {
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000506 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
507 PN->addIncoming(InVal, *PI);
Chris Lattnerc3748562007-04-02 01:35:34 +0000508 }
509 }
510 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000511
Chris Lattnerc3748562007-04-02 01:35:34 +0000512 // The PHIs are now updated, change everything that refers to BB to use
513 // DestBB and remove BB.
514 BB->replaceAllUsesWith(DestBB);
515 BB->eraseFromParent();
Cameron Zwarichced753f2011-01-05 17:27:27 +0000516 ++NumBlocksElim;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000517
David Greene74e2d492010-01-05 01:27:11 +0000518 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
Chris Lattnerc3748562007-04-02 01:35:34 +0000519}
520
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000521// Computes a map of base pointer relocation instructions to corresponding
522// derived pointer relocation instructions given a vector of all relocate calls
523static void computeBaseDerivedRelocateMap(
524 const SmallVectorImpl<User *> &AllRelocateCalls,
525 DenseMap<IntrinsicInst *, SmallVector<IntrinsicInst *, 2>> &
526 RelocateInstMap) {
527 // Collect information in two maps: one primarily for locating the base object
528 // while filling the second map; the second map is the final structure holding
529 // a mapping between Base and corresponding Derived relocate calls
530 DenseMap<std::pair<unsigned, unsigned>, IntrinsicInst *> RelocateIdxMap;
531 for (auto &U : AllRelocateCalls) {
532 GCRelocateOperands ThisRelocate(U);
533 IntrinsicInst *I = cast<IntrinsicInst>(U);
Sanjoy Das499d7032015-05-06 02:36:26 +0000534 auto K = std::make_pair(ThisRelocate.getBasePtrIndex(),
535 ThisRelocate.getDerivedPtrIndex());
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000536 RelocateIdxMap.insert(std::make_pair(K, I));
537 }
538 for (auto &Item : RelocateIdxMap) {
539 std::pair<unsigned, unsigned> Key = Item.first;
540 if (Key.first == Key.second)
541 // Base relocation: nothing to insert
542 continue;
543
544 IntrinsicInst *I = Item.second;
545 auto BaseKey = std::make_pair(Key.first, Key.first);
Sanjoy Dasb8186762015-02-27 02:24:16 +0000546
547 // We're iterating over RelocateIdxMap so we cannot modify it.
548 auto MaybeBase = RelocateIdxMap.find(BaseKey);
549 if (MaybeBase == RelocateIdxMap.end())
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000550 // TODO: We might want to insert a new base object relocate and gep off
551 // that, if there are enough derived object relocates.
552 continue;
Sanjoy Dasb8186762015-02-27 02:24:16 +0000553
554 RelocateInstMap[MaybeBase->second].push_back(I);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000555 }
556}
557
558// Accepts a GEP and extracts the operands into a vector provided they're all
559// small integer constants
560static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP,
561 SmallVectorImpl<Value *> &OffsetV) {
562 for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
563 // Only accept small constant integer operands
564 auto Op = dyn_cast<ConstantInt>(GEP->getOperand(i));
565 if (!Op || Op->getZExtValue() > 20)
566 return false;
567 }
568
569 for (unsigned i = 1; i < GEP->getNumOperands(); i++)
570 OffsetV.push_back(GEP->getOperand(i));
571 return true;
572}
573
574// Takes a RelocatedBase (base pointer relocation instruction) and Targets to
575// replace, computes a replacement, and affects it.
576static bool
577simplifyRelocatesOffABase(IntrinsicInst *RelocatedBase,
578 const SmallVectorImpl<IntrinsicInst *> &Targets) {
579 bool MadeChange = false;
580 for (auto &ToReplace : Targets) {
581 GCRelocateOperands MasterRelocate(RelocatedBase);
582 GCRelocateOperands ThisRelocate(ToReplace);
583
Sanjoy Das499d7032015-05-06 02:36:26 +0000584 assert(ThisRelocate.getBasePtrIndex() == MasterRelocate.getBasePtrIndex() &&
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000585 "Not relocating a derived object of the original base object");
Sanjoy Das499d7032015-05-06 02:36:26 +0000586 if (ThisRelocate.getBasePtrIndex() == ThisRelocate.getDerivedPtrIndex()) {
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000587 // A duplicate relocate call. TODO: coalesce duplicates.
588 continue;
589 }
590
Sanjoy Das499d7032015-05-06 02:36:26 +0000591 Value *Base = ThisRelocate.getBasePtr();
592 auto Derived = dyn_cast<GetElementPtrInst>(ThisRelocate.getDerivedPtr());
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000593 if (!Derived || Derived->getPointerOperand() != Base)
594 continue;
595
596 SmallVector<Value *, 2> OffsetV;
597 if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV))
598 continue;
599
600 // Create a Builder and replace the target callsite with a gep
Sanjoy Das3d705e32015-05-11 23:47:30 +0000601 assert(RelocatedBase->getNextNode() && "Should always have one since it's not a terminator");
602
603 // Insert after RelocatedBase
604 IRBuilder<> Builder(RelocatedBase->getNextNode());
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000605 Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc());
Sanjoy Das89c54912015-05-11 18:49:34 +0000606
607 // If gc_relocate does not match the actual type, cast it to the right type.
608 // In theory, there must be a bitcast after gc_relocate if the type does not
609 // match, and we should reuse it to get the derived pointer. But it could be
610 // cases like this:
611 // bb1:
612 // ...
613 // %g1 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
614 // br label %merge
615 //
616 // bb2:
617 // ...
618 // %g2 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
619 // br label %merge
620 //
621 // merge:
622 // %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ]
623 // %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)*
624 //
625 // In this case, we can not find the bitcast any more. So we insert a new bitcast
626 // no matter there is already one or not. In this way, we can handle all cases, and
627 // the extra bitcast should be optimized away in later passes.
628 Instruction *ActualRelocatedBase = RelocatedBase;
629 if (RelocatedBase->getType() != Base->getType()) {
630 ActualRelocatedBase =
631 cast<Instruction>(Builder.CreateBitCast(RelocatedBase, Base->getType()));
Sanjoy Das89c54912015-05-11 18:49:34 +0000632 }
David Blaikie68d535c2015-03-24 22:38:16 +0000633 Value *Replacement = Builder.CreateGEP(
Sanjoy Das89c54912015-05-11 18:49:34 +0000634 Derived->getSourceElementType(), ActualRelocatedBase, makeArrayRef(OffsetV));
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000635 Instruction *ReplacementInst = cast<Instruction>(Replacement);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000636 Replacement->takeName(ToReplace);
Sanjoy Das89c54912015-05-11 18:49:34 +0000637 // If the newly generated derived pointer's type does not match the original derived
638 // pointer's type, cast the new derived pointer to match it. Same reasoning as above.
639 Instruction *ActualReplacement = ReplacementInst;
640 if (ReplacementInst->getType() != ToReplace->getType()) {
641 ActualReplacement =
642 cast<Instruction>(Builder.CreateBitCast(ReplacementInst, ToReplace->getType()));
Sanjoy Das89c54912015-05-11 18:49:34 +0000643 }
644 ToReplace->replaceAllUsesWith(ActualReplacement);
Ramkumar Ramachandradba73292015-01-14 23:27:07 +0000645 ToReplace->eraseFromParent();
646
647 MadeChange = true;
648 }
649 return MadeChange;
650}
651
652// Turns this:
653//
654// %base = ...
655// %ptr = gep %base + 15
656// %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
657// %base' = relocate(%tok, i32 4, i32 4)
658// %ptr' = relocate(%tok, i32 4, i32 5)
659// %val = load %ptr'
660//
661// into this:
662//
663// %base = ...
664// %ptr = gep %base + 15
665// %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
666// %base' = gc.relocate(%tok, i32 4, i32 4)
667// %ptr' = gep %base' + 15
668// %val = load %ptr'
669bool CodeGenPrepare::simplifyOffsetableRelocate(Instruction &I) {
670 bool MadeChange = false;
671 SmallVector<User *, 2> AllRelocateCalls;
672
673 for (auto *U : I.users())
674 if (isGCRelocate(dyn_cast<Instruction>(U)))
675 // Collect all the relocate calls associated with a statepoint
676 AllRelocateCalls.push_back(U);
677
678 // We need atleast one base pointer relocation + one derived pointer
679 // relocation to mangle
680 if (AllRelocateCalls.size() < 2)
681 return false;
682
683 // RelocateInstMap is a mapping from the base relocate instruction to the
684 // corresponding derived relocate instructions
685 DenseMap<IntrinsicInst *, SmallVector<IntrinsicInst *, 2>> RelocateInstMap;
686 computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap);
687 if (RelocateInstMap.empty())
688 return false;
689
690 for (auto &Item : RelocateInstMap)
691 // Item.first is the RelocatedBase to offset against
692 // Item.second is the vector of Targets to replace
693 MadeChange = simplifyRelocatesOffABase(Item.first, Item.second);
694 return MadeChange;
695}
696
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000697/// SinkCast - Sink the specified cast instruction into its user blocks
698static bool SinkCast(CastInst *CI) {
Chris Lattnerf2836d12007-03-31 04:06:36 +0000699 BasicBlock *DefBB = CI->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000700
Chris Lattnerf2836d12007-03-31 04:06:36 +0000701 /// InsertedCasts - Only insert a cast in each block once.
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000702 DenseMap<BasicBlock*, CastInst*> InsertedCasts;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000703
Chris Lattnerf2836d12007-03-31 04:06:36 +0000704 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000705 for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
Chris Lattnerf2836d12007-03-31 04:06:36 +0000706 UI != E; ) {
707 Use &TheUse = UI.getUse();
708 Instruction *User = cast<Instruction>(*UI);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000709
Chris Lattnerf2836d12007-03-31 04:06:36 +0000710 // Figure out which BB this cast is used in. For PHI's this is the
711 // appropriate predecessor block.
712 BasicBlock *UserBB = User->getParent();
713 if (PHINode *PN = dyn_cast<PHINode>(User)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000714 UserBB = PN->getIncomingBlock(TheUse);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000715 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000716
Chris Lattnerf2836d12007-03-31 04:06:36 +0000717 // Preincrement use iterator so we don't invalidate it.
718 ++UI;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000719
Chris Lattnerf2836d12007-03-31 04:06:36 +0000720 // If this user is in the same block as the cast, don't change the cast.
721 if (UserBB == DefBB) continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000722
Chris Lattnerf2836d12007-03-31 04:06:36 +0000723 // If we have already inserted a cast into this block, use it.
724 CastInst *&InsertedCast = InsertedCasts[UserBB];
725
726 if (!InsertedCast) {
Bill Wendling8ddfc092011-08-16 20:45:24 +0000727 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000728 InsertedCast =
729 CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
Chris Lattnerf2836d12007-03-31 04:06:36 +0000730 InsertPt);
Chris Lattnerf2836d12007-03-31 04:06:36 +0000731 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000732
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000733 // Replace a use of the cast with a use of the new cast.
Chris Lattnerf2836d12007-03-31 04:06:36 +0000734 TheUse = InsertedCast;
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000735 MadeChange = true;
Cameron Zwarichced753f2011-01-05 17:27:27 +0000736 ++NumCastUses;
Chris Lattnerf2836d12007-03-31 04:06:36 +0000737 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000738
Chris Lattnerf2836d12007-03-31 04:06:36 +0000739 // If we removed all uses, nuke the cast.
Duncan Sandsafa84da42008-01-20 16:51:46 +0000740 if (CI->use_empty()) {
Chris Lattnerf2836d12007-03-31 04:06:36 +0000741 CI->eraseFromParent();
Duncan Sandsafa84da42008-01-20 16:51:46 +0000742 MadeChange = true;
743 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000744
Chris Lattnerf2836d12007-03-31 04:06:36 +0000745 return MadeChange;
746}
747
Manuel Jacoba7c48f92014-03-13 13:36:25 +0000748/// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
749/// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
750/// sink it into user blocks to reduce the number of virtual
751/// registers that must be created and coalesced.
752///
753/// Return true if any changes are made.
754///
755static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
756 // If this is a noop copy,
757 EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
758 EVT DstVT = TLI.getValueType(CI->getType());
759
760 // This is an fp<->int conversion?
761 if (SrcVT.isInteger() != DstVT.isInteger())
762 return false;
763
764 // If this is an extension, it will be a zero or sign extension, which
765 // isn't a noop.
766 if (SrcVT.bitsLT(DstVT)) return false;
767
768 // If these values will be promoted, find out what they will be promoted
769 // to. This helps us consider truncates on PPC as noop copies when they
770 // are.
771 if (TLI.getTypeAction(CI->getContext(), SrcVT) ==
772 TargetLowering::TypePromoteInteger)
773 SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
774 if (TLI.getTypeAction(CI->getContext(), DstVT) ==
775 TargetLowering::TypePromoteInteger)
776 DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
777
778 // If, after promotion, these are the same types, this is a noop copy.
779 if (SrcVT != DstVT)
780 return false;
781
782 return SinkCast(CI);
783}
784
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000785/// CombineUAddWithOverflow - try to combine CI into a call to the
786/// llvm.uadd.with.overflow intrinsic if possible.
787///
788/// Return true if any changes were made.
789static bool CombineUAddWithOverflow(CmpInst *CI) {
790 Value *A, *B;
791 Instruction *AddI;
792 if (!match(CI,
793 m_UAddWithOverflow(m_Value(A), m_Value(B), m_Instruction(AddI))))
794 return false;
795
796 Type *Ty = AddI->getType();
797 if (!isa<IntegerType>(Ty))
798 return false;
799
800 // We don't want to move around uses of condition values this late, so we we
801 // check if it is legal to create the call to the intrinsic in the basic
802 // block containing the icmp:
803
804 if (AddI->getParent() != CI->getParent() && !AddI->hasOneUse())
805 return false;
806
807#ifndef NDEBUG
808 // Someday m_UAddWithOverflow may get smarter, but this is a safe assumption
809 // for now:
810 if (AddI->hasOneUse())
811 assert(*AddI->user_begin() == CI && "expected!");
812#endif
813
814 Module *M = CI->getParent()->getParent()->getParent();
815 Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty);
816
817 auto *InsertPt = AddI->hasOneUse() ? CI : AddI;
818
819 auto *UAddWithOverflow =
820 CallInst::Create(F, {A, B}, "uadd.overflow", InsertPt);
821 auto *UAdd = ExtractValueInst::Create(UAddWithOverflow, 0, "uadd", InsertPt);
822 auto *Overflow =
823 ExtractValueInst::Create(UAddWithOverflow, 1, "overflow", InsertPt);
824
825 CI->replaceAllUsesWith(Overflow);
826 AddI->replaceAllUsesWith(UAdd);
827 CI->eraseFromParent();
828 AddI->eraseFromParent();
829 return true;
830}
831
832/// SinkCmpExpression - Sink the given CmpInst into user blocks to reduce
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000833/// the number of virtual registers that must be created and coalesced. This is
Chris Lattner27406942007-08-02 16:53:43 +0000834/// a clear win except on targets with multiple condition code registers
835/// (PowerPC), where it might lose; some adjustment may be wanted there.
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000836///
837/// Return true if any changes are made.
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000838static bool SinkCmpExpression(CmpInst *CI) {
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000839 BasicBlock *DefBB = CI->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000840
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000841 /// InsertedCmp - Only insert a cmp in each block once.
842 DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000843
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000844 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000845 for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000846 UI != E; ) {
847 Use &TheUse = UI.getUse();
848 Instruction *User = cast<Instruction>(*UI);
Eric Christopherc1ea1492008-09-24 05:32:41 +0000849
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000850 // Preincrement use iterator so we don't invalidate it.
851 ++UI;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000852
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000853 // Don't bother for PHI nodes.
854 if (isa<PHINode>(User))
855 continue;
856
857 // Figure out which BB this cmp is used in.
858 BasicBlock *UserBB = User->getParent();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000859
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000860 // If this user is in the same block as the cmp, don't change the cmp.
861 if (UserBB == DefBB) continue;
Eric Christopherc1ea1492008-09-24 05:32:41 +0000862
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000863 // If we have already inserted a cmp into this block, use it.
864 CmpInst *&InsertedCmp = InsertedCmps[UserBB];
865
866 if (!InsertedCmp) {
Bill Wendling8ddfc092011-08-16 20:45:24 +0000867 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Eric Christopherc1ea1492008-09-24 05:32:41 +0000868 InsertedCmp =
Dan Gohmanad1f0a12009-08-25 23:17:54 +0000869 CmpInst::Create(CI->getOpcode(),
Owen Anderson1e5f00e2009-07-09 23:48:35 +0000870 CI->getPredicate(), CI->getOperand(0),
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000871 CI->getOperand(1), "", InsertPt);
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000872 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000873
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000874 // Replace a use of the cmp with a use of the new cmp.
875 TheUse = InsertedCmp;
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000876 MadeChange = true;
Cameron Zwarichced753f2011-01-05 17:27:27 +0000877 ++NumCmpUses;
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000878 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000879
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000880 // If we removed all uses, nuke the cmp.
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000881 if (CI->use_empty()) {
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000882 CI->eraseFromParent();
Benjamin Kramerb4bf14c2015-04-10 22:25:36 +0000883 MadeChange = true;
884 }
Eric Christopherc1ea1492008-09-24 05:32:41 +0000885
Dale Johannesenedfec0b2007-06-12 16:50:17 +0000886 return MadeChange;
887}
888
Sanjoy Dasb6c59142015-04-10 21:07:09 +0000889static bool OptimizeCmpExpression(CmpInst *CI) {
890 if (SinkCmpExpression(CI))
891 return true;
892
893 if (CombineUAddWithOverflow(CI))
894 return true;
895
896 return false;
897}
898
Yi Jiangd069f632014-04-21 19:34:27 +0000899/// isExtractBitsCandidateUse - Check if the candidates could
900/// be combined with shift instruction, which includes:
901/// 1. Truncate instruction
902/// 2. And instruction and the imm is a mask of the low bits:
903/// imm & (imm+1) == 0
Benjamin Kramer322053c2014-04-27 14:54:59 +0000904static bool isExtractBitsCandidateUse(Instruction *User) {
Yi Jiangd069f632014-04-21 19:34:27 +0000905 if (!isa<TruncInst>(User)) {
906 if (User->getOpcode() != Instruction::And ||
907 !isa<ConstantInt>(User->getOperand(1)))
908 return false;
909
Quentin Colombetd4f44692014-04-22 01:20:34 +0000910 const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue();
Yi Jiangd069f632014-04-21 19:34:27 +0000911
Quentin Colombetd4f44692014-04-22 01:20:34 +0000912 if ((Cimm & (Cimm + 1)).getBoolValue())
Yi Jiangd069f632014-04-21 19:34:27 +0000913 return false;
914 }
915 return true;
916}
917
918/// SinkShiftAndTruncate - sink both shift and truncate instruction
919/// to the use of truncate's BB.
Benjamin Kramer322053c2014-04-27 14:54:59 +0000920static bool
Yi Jiangd069f632014-04-21 19:34:27 +0000921SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
922 DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts,
923 const TargetLowering &TLI) {
924 BasicBlock *UserBB = User->getParent();
925 DenseMap<BasicBlock *, CastInst *> InsertedTruncs;
926 TruncInst *TruncI = dyn_cast<TruncInst>(User);
927 bool MadeChange = false;
928
929 for (Value::user_iterator TruncUI = TruncI->user_begin(),
930 TruncE = TruncI->user_end();
931 TruncUI != TruncE;) {
932
933 Use &TruncTheUse = TruncUI.getUse();
934 Instruction *TruncUser = cast<Instruction>(*TruncUI);
935 // Preincrement use iterator so we don't invalidate it.
936
937 ++TruncUI;
938
939 int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode());
940 if (!ISDOpcode)
941 continue;
942
Tim Northovere2239ff2014-07-29 10:20:22 +0000943 // If the use is actually a legal node, there will not be an
944 // implicit truncate.
945 // FIXME: always querying the result type is just an
946 // approximation; some nodes' legality is determined by the
947 // operand or other means. There's no good way to find out though.
Ahmed Bougacha0788d492014-11-12 22:16:55 +0000948 if (TLI.isOperationLegalOrCustom(
949 ISDOpcode, TLI.getValueType(TruncUser->getType(), true)))
Yi Jiangd069f632014-04-21 19:34:27 +0000950 continue;
951
952 // Don't bother for PHI nodes.
953 if (isa<PHINode>(TruncUser))
954 continue;
955
956 BasicBlock *TruncUserBB = TruncUser->getParent();
957
958 if (UserBB == TruncUserBB)
959 continue;
960
961 BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB];
962 CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB];
963
964 if (!InsertedShift && !InsertedTrunc) {
965 BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt();
966 // Sink the shift
967 if (ShiftI->getOpcode() == Instruction::AShr)
968 InsertedShift =
969 BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt);
970 else
971 InsertedShift =
972 BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt);
973
974 // Sink the trunc
975 BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt();
976 TruncInsertPt++;
977
978 InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift,
979 TruncI->getType(), "", TruncInsertPt);
980
981 MadeChange = true;
982
983 TruncTheUse = InsertedTrunc;
984 }
985 }
986 return MadeChange;
987}
988
989/// OptimizeExtractBits - sink the shift *right* instruction into user blocks if
990/// the uses could potentially be combined with this shift instruction and
991/// generate BitExtract instruction. It will only be applied if the architecture
992/// supports BitExtract instruction. Here is an example:
993/// BB1:
994/// %x.extract.shift = lshr i64 %arg1, 32
995/// BB2:
996/// %x.extract.trunc = trunc i64 %x.extract.shift to i16
997/// ==>
998///
999/// BB2:
1000/// %x.extract.shift.1 = lshr i64 %arg1, 32
1001/// %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16
1002///
1003/// CodeGen will recoginze the pattern in BB2 and generate BitExtract
1004/// instruction.
1005/// Return true if any changes are made.
1006static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI,
1007 const TargetLowering &TLI) {
1008 BasicBlock *DefBB = ShiftI->getParent();
1009
1010 /// Only insert instructions in each block once.
1011 DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts;
1012
1013 bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(ShiftI->getType()));
1014
1015 bool MadeChange = false;
1016 for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end();
1017 UI != E;) {
1018 Use &TheUse = UI.getUse();
1019 Instruction *User = cast<Instruction>(*UI);
1020 // Preincrement use iterator so we don't invalidate it.
1021 ++UI;
1022
1023 // Don't bother for PHI nodes.
1024 if (isa<PHINode>(User))
1025 continue;
1026
1027 if (!isExtractBitsCandidateUse(User))
1028 continue;
1029
1030 BasicBlock *UserBB = User->getParent();
1031
1032 if (UserBB == DefBB) {
1033 // If the shift and truncate instruction are in the same BB. The use of
1034 // the truncate(TruncUse) may still introduce another truncate if not
1035 // legal. In this case, we would like to sink both shift and truncate
1036 // instruction to the BB of TruncUse.
1037 // for example:
1038 // BB1:
1039 // i64 shift.result = lshr i64 opnd, imm
1040 // trunc.result = trunc shift.result to i16
1041 //
1042 // BB2:
1043 // ----> We will have an implicit truncate here if the architecture does
1044 // not have i16 compare.
1045 // cmp i16 trunc.result, opnd2
1046 //
1047 if (isa<TruncInst>(User) && shiftIsLegal
1048 // If the type of the truncate is legal, no trucate will be
1049 // introduced in other basic blocks.
1050 && (!TLI.isTypeLegal(TLI.getValueType(User->getType()))))
1051 MadeChange =
1052 SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI);
1053
1054 continue;
1055 }
1056 // If we have already inserted a shift into this block, use it.
1057 BinaryOperator *&InsertedShift = InsertedShifts[UserBB];
1058
1059 if (!InsertedShift) {
1060 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
1061
1062 if (ShiftI->getOpcode() == Instruction::AShr)
1063 InsertedShift =
1064 BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt);
1065 else
1066 InsertedShift =
1067 BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt);
1068
1069 MadeChange = true;
1070 }
1071
1072 // Replace a use of the shift with a use of the new shift.
1073 TheUse = InsertedShift;
1074 }
1075
1076 // If we removed all uses, nuke the shift.
1077 if (ShiftI->use_empty())
1078 ShiftI->eraseFromParent();
1079
1080 return MadeChange;
1081}
1082
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001083// ScalarizeMaskedLoad() translates masked load intrinsic, like
1084// <16 x i32 > @llvm.masked.load( <16 x i32>* %addr, i32 align,
1085// <16 x i1> %mask, <16 x i32> %passthru)
1086// to a chain of basic blocks, whith loading element one-by-one if
1087// the appropriate mask bit is set
1088//
1089// %1 = bitcast i8* %addr to i32*
1090// %2 = extractelement <16 x i1> %mask, i32 0
1091// %3 = icmp eq i1 %2, true
1092// br i1 %3, label %cond.load, label %else
1093//
1094//cond.load: ; preds = %0
1095// %4 = getelementptr i32* %1, i32 0
1096// %5 = load i32* %4
1097// %6 = insertelement <16 x i32> undef, i32 %5, i32 0
1098// br label %else
1099//
1100//else: ; preds = %0, %cond.load
1101// %res.phi.else = phi <16 x i32> [ %6, %cond.load ], [ undef, %0 ]
1102// %7 = extractelement <16 x i1> %mask, i32 1
1103// %8 = icmp eq i1 %7, true
1104// br i1 %8, label %cond.load1, label %else2
1105//
1106//cond.load1: ; preds = %else
1107// %9 = getelementptr i32* %1, i32 1
1108// %10 = load i32* %9
1109// %11 = insertelement <16 x i32> %res.phi.else, i32 %10, i32 1
1110// br label %else2
1111//
1112//else2: ; preds = %else, %cond.load1
1113// %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ]
1114// %12 = extractelement <16 x i1> %mask, i32 2
1115// %13 = icmp eq i1 %12, true
1116// br i1 %13, label %cond.load4, label %else5
1117//
1118static void ScalarizeMaskedLoad(CallInst *CI) {
1119 Value *Ptr = CI->getArgOperand(0);
1120 Value *Src0 = CI->getArgOperand(3);
1121 Value *Mask = CI->getArgOperand(2);
1122 VectorType *VecType = dyn_cast<VectorType>(CI->getType());
1123 Type *EltTy = VecType->getElementType();
1124
1125 assert(VecType && "Unexpected return type of masked load intrinsic");
1126
1127 IRBuilder<> Builder(CI->getContext());
1128 Instruction *InsertPt = CI;
1129 BasicBlock *IfBlock = CI->getParent();
1130 BasicBlock *CondBlock = nullptr;
1131 BasicBlock *PrevIfBlock = CI->getParent();
1132 Builder.SetInsertPoint(InsertPt);
1133
1134 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1135
1136 // Bitcast %addr fron i8* to EltTy*
1137 Type *NewPtrType =
1138 EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace());
1139 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
1140 Value *UndefVal = UndefValue::get(VecType);
1141
1142 // The result vector
1143 Value *VResult = UndefVal;
1144
1145 PHINode *Phi = nullptr;
1146 Value *PrevPhi = UndefVal;
1147
1148 unsigned VectorWidth = VecType->getNumElements();
1149 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1150
1151 // Fill the "else" block, created in the previous iteration
1152 //
1153 // %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ]
1154 // %mask_1 = extractelement <16 x i1> %mask, i32 Idx
1155 // %to_load = icmp eq i1 %mask_1, true
1156 // br i1 %to_load, label %cond.load, label %else
1157 //
1158 if (Idx > 0) {
1159 Phi = Builder.CreatePHI(VecType, 2, "res.phi.else");
1160 Phi->addIncoming(VResult, CondBlock);
1161 Phi->addIncoming(PrevPhi, PrevIfBlock);
1162 PrevPhi = Phi;
1163 VResult = Phi;
1164 }
1165
1166 Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
1167 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1168 ConstantInt::get(Predicate->getType(), 1));
1169
1170 // Create "cond" block
1171 //
1172 // %EltAddr = getelementptr i32* %1, i32 0
1173 // %Elt = load i32* %EltAddr
1174 // VResult = insertelement <16 x i32> VResult, i32 %Elt, i32 Idx
1175 //
1176 CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.load");
1177 Builder.SetInsertPoint(InsertPt);
David Blaikieaa41cd52015-04-03 21:33:42 +00001178
1179 Value *Gep =
1180 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001181 LoadInst* Load = Builder.CreateLoad(Gep, false);
1182 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
1183
1184 // Create "else" block, fill it in the next iteration
1185 BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else");
1186 Builder.SetInsertPoint(InsertPt);
1187 Instruction *OldBr = IfBlock->getTerminator();
1188 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1189 OldBr->eraseFromParent();
1190 PrevIfBlock = IfBlock;
1191 IfBlock = NewIfBlock;
1192 }
1193
1194 Phi = Builder.CreatePHI(VecType, 2, "res.phi.select");
1195 Phi->addIncoming(VResult, CondBlock);
1196 Phi->addIncoming(PrevPhi, PrevIfBlock);
1197 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
1198 CI->replaceAllUsesWith(NewI);
1199 CI->eraseFromParent();
1200}
1201
1202// ScalarizeMaskedStore() translates masked store intrinsic, like
1203// void @llvm.masked.store(<16 x i32> %src, <16 x i32>* %addr, i32 align,
1204// <16 x i1> %mask)
1205// to a chain of basic blocks, that stores element one-by-one if
1206// the appropriate mask bit is set
1207//
1208// %1 = bitcast i8* %addr to i32*
1209// %2 = extractelement <16 x i1> %mask, i32 0
1210// %3 = icmp eq i1 %2, true
1211// br i1 %3, label %cond.store, label %else
1212//
1213// cond.store: ; preds = %0
1214// %4 = extractelement <16 x i32> %val, i32 0
1215// %5 = getelementptr i32* %1, i32 0
1216// store i32 %4, i32* %5
1217// br label %else
1218//
1219// else: ; preds = %0, %cond.store
1220// %6 = extractelement <16 x i1> %mask, i32 1
1221// %7 = icmp eq i1 %6, true
1222// br i1 %7, label %cond.store1, label %else2
1223//
1224// cond.store1: ; preds = %else
1225// %8 = extractelement <16 x i32> %val, i32 1
1226// %9 = getelementptr i32* %1, i32 1
1227// store i32 %8, i32* %9
1228// br label %else2
1229// . . .
1230static void ScalarizeMaskedStore(CallInst *CI) {
1231 Value *Ptr = CI->getArgOperand(1);
1232 Value *Src = CI->getArgOperand(0);
1233 Value *Mask = CI->getArgOperand(3);
1234
1235 VectorType *VecType = dyn_cast<VectorType>(Src->getType());
1236 Type *EltTy = VecType->getElementType();
1237
1238 assert(VecType && "Unexpected data type in masked store intrinsic");
1239
1240 IRBuilder<> Builder(CI->getContext());
1241 Instruction *InsertPt = CI;
1242 BasicBlock *IfBlock = CI->getParent();
1243 Builder.SetInsertPoint(InsertPt);
1244 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1245
1246 // Bitcast %addr fron i8* to EltTy*
1247 Type *NewPtrType =
1248 EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace());
1249 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
1250
1251 unsigned VectorWidth = VecType->getNumElements();
1252 for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
1253
1254 // Fill the "else" block, created in the previous iteration
1255 //
1256 // %mask_1 = extractelement <16 x i1> %mask, i32 Idx
1257 // %to_store = icmp eq i1 %mask_1, true
1258 // br i1 %to_load, label %cond.store, label %else
1259 //
1260 Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
1261 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate,
1262 ConstantInt::get(Predicate->getType(), 1));
1263
1264 // Create "cond" block
1265 //
1266 // %OneElt = extractelement <16 x i32> %Src, i32 Idx
1267 // %EltAddr = getelementptr i32* %1, i32 0
1268 // %store i32 %OneElt, i32* %EltAddr
1269 //
1270 BasicBlock *CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.store");
1271 Builder.SetInsertPoint(InsertPt);
1272
1273 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
David Blaikieaa41cd52015-04-03 21:33:42 +00001274 Value *Gep =
1275 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001276 Builder.CreateStore(OneElt, Gep);
1277
1278 // Create "else" block, fill it in the next iteration
1279 BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else");
1280 Builder.SetInsertPoint(InsertPt);
1281 Instruction *OldBr = IfBlock->getTerminator();
1282 BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr);
1283 OldBr->eraseFromParent();
1284 IfBlock = NewIfBlock;
1285 }
1286 CI->eraseFromParent();
1287}
1288
1289bool CodeGenPrepare::OptimizeCallInst(CallInst *CI, bool& ModifiedDT) {
Chris Lattner7a277142011-01-15 07:14:54 +00001290 BasicBlock *BB = CI->getParent();
Nadav Rotem465834c2012-07-24 10:51:42 +00001291
Chris Lattner7a277142011-01-15 07:14:54 +00001292 // Lower inline assembly if we can.
1293 // If we found an inline asm expession, and if the target knows how to
1294 // lower it to normal LLVM code, do so now.
1295 if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
1296 if (TLI->ExpandInlineAsm(CI)) {
1297 // Avoid invalidating the iterator.
1298 CurInstIterator = BB->begin();
1299 // Avoid processing instructions out of order, which could cause
1300 // reuse before a value is defined.
1301 SunkAddrs.clear();
1302 return true;
1303 }
1304 // Sink address computing for memory operands into the block.
1305 if (OptimizeInlineAsmInst(CI))
1306 return true;
1307 }
Nadav Rotem465834c2012-07-24 10:51:42 +00001308
John Brawn0dbcd652015-03-18 12:01:59 +00001309 const DataLayout *TD = TLI ? TLI->getDataLayout() : nullptr;
1310
1311 // Align the pointer arguments to this call if the target thinks it's a good
1312 // idea
1313 unsigned MinSize, PrefAlign;
1314 if (TLI && TD && TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) {
1315 for (auto &Arg : CI->arg_operands()) {
1316 // We want to align both objects whose address is used directly and
1317 // objects whose address is used in casts and GEPs, though it only makes
1318 // sense for GEPs if the offset is a multiple of the desired alignment and
1319 // if size - offset meets the size threshold.
1320 if (!Arg->getType()->isPointerTy())
1321 continue;
1322 APInt Offset(TD->getPointerSizeInBits(
1323 cast<PointerType>(Arg->getType())->getAddressSpace()), 0);
1324 Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*TD, Offset);
1325 uint64_t Offset2 = Offset.getLimitedValue();
John Brawne8fd6c82015-04-13 10:47:39 +00001326 if ((Offset2 & (PrefAlign-1)) != 0)
1327 continue;
John Brawn0dbcd652015-03-18 12:01:59 +00001328 AllocaInst *AI;
John Brawne8fd6c82015-04-13 10:47:39 +00001329 if ((AI = dyn_cast<AllocaInst>(Val)) &&
John Brawn0dbcd652015-03-18 12:01:59 +00001330 AI->getAlignment() < PrefAlign &&
1331 TD->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2)
1332 AI->setAlignment(PrefAlign);
John Brawne8fd6c82015-04-13 10:47:39 +00001333 // Global variables can only be aligned if they are defined in this
1334 // object (i.e. they are uniquely initialized in this object), and
1335 // over-aligning global variables that have an explicit section is
1336 // forbidden.
1337 GlobalVariable *GV;
1338 if ((GV = dyn_cast<GlobalVariable>(Val)) &&
1339 GV->hasUniqueInitializer() &&
1340 !GV->hasSection() &&
1341 GV->getAlignment() < PrefAlign &&
1342 TD->getTypeAllocSize(
1343 GV->getType()->getElementType()) >= MinSize + Offset2)
1344 GV->setAlignment(PrefAlign);
John Brawn0dbcd652015-03-18 12:01:59 +00001345 }
1346 // If this is a memcpy (or similar) then we may be able to improve the
1347 // alignment
1348 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) {
1349 unsigned Align = getKnownAlignment(MI->getDest(), *TD);
1350 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
1351 Align = std::min(Align, getKnownAlignment(MTI->getSource(), *TD));
1352 if (Align > MI->getAlignment())
1353 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), Align));
1354 }
1355 }
1356
Eric Christopher4b7948e2010-03-11 02:41:03 +00001357 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001358 if (II) {
1359 switch (II->getIntrinsicID()) {
1360 default: break;
1361 case Intrinsic::objectsize: {
1362 // Lower all uses of llvm.objectsize.*
1363 bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
1364 Type *ReturnTy = CI->getType();
1365 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
Nadav Rotem465834c2012-07-24 10:51:42 +00001366
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001367 // Substituting this can cause recursive simplifications, which can
1368 // invalidate our iterator. Use a WeakVH to hold onto it in case this
1369 // happens.
1370 WeakVH IterHandle(CurInstIterator);
Nadav Rotem465834c2012-07-24 10:51:42 +00001371
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001372 replaceAndRecursivelySimplify(CI, RetVal,
Quentin Colombet7bdd50d2015-03-18 23:17:28 +00001373 TLInfo, nullptr);
Chris Lattner1b93be52011-01-15 07:25:29 +00001374
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001375 // If the iterator instruction was recursively deleted, start over at the
1376 // start of the block.
1377 if (IterHandle != CurInstIterator) {
1378 CurInstIterator = BB->begin();
1379 SunkAddrs.clear();
1380 }
1381 return true;
Chris Lattner86d56c62011-01-18 20:53:04 +00001382 }
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001383 case Intrinsic::masked_load: {
1384 // Scalarize unsupported vector masked load
1385 if (!TTI->isLegalMaskedLoad(CI->getType(), 1)) {
1386 ScalarizeMaskedLoad(CI);
1387 ModifiedDT = true;
1388 return true;
1389 }
1390 return false;
1391 }
1392 case Intrinsic::masked_store: {
1393 if (!TTI->isLegalMaskedStore(CI->getArgOperand(0)->getType(), 1)) {
1394 ScalarizeMaskedStore(CI);
1395 ModifiedDT = true;
1396 return true;
1397 }
1398 return false;
1399 }
1400 }
Eric Christopher4b7948e2010-03-11 02:41:03 +00001401
Elena Demikhovsky87700a72014-12-28 08:54:45 +00001402 if (TLI) {
1403 SmallVector<Value*, 2> PtrOps;
1404 Type *AccessTy;
1405 if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy))
1406 while (!PtrOps.empty())
1407 if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy))
1408 return true;
1409 }
Pete Cooper615fd892012-03-13 20:59:56 +00001410 }
1411
Eric Christopher4b7948e2010-03-11 02:41:03 +00001412 // From here on out we're working with named functions.
Craig Topperc0196b12014-04-14 00:51:57 +00001413 if (!CI->getCalledFunction()) return false;
Devang Patel0da52502011-05-26 21:51:06 +00001414
Benjamin Kramer7b88a492010-03-12 09:27:41 +00001415 // Lower all default uses of _chk calls. This is very similar
1416 // to what InstCombineCalls does, but here we are only lowering calls
Ahmed Bougachae03bef72015-01-12 17:22:43 +00001417 // to fortified library functions (e.g. __memcpy_chk) that have the default
1418 // "don't know" as the objectsize. Anything else should be left alone.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001419 FortifiedLibCallSimplifier Simplifier(TLInfo, true);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00001420 if (Value *V = Simplifier.optimizeCall(CI)) {
1421 CI->replaceAllUsesWith(V);
1422 CI->eraseFromParent();
1423 return true;
1424 }
1425 return false;
Eric Christopher4b7948e2010-03-11 02:41:03 +00001426}
Chris Lattner1b93be52011-01-15 07:25:29 +00001427
Evan Cheng0663f232011-03-21 01:19:09 +00001428/// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
1429/// instructions to the predecessor to enable tail call optimizations. The
1430/// case it is currently looking for is:
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001431/// @code
Evan Cheng0663f232011-03-21 01:19:09 +00001432/// bb0:
1433/// %tmp0 = tail call i32 @f0()
1434/// br label %return
1435/// bb1:
1436/// %tmp1 = tail call i32 @f1()
1437/// br label %return
1438/// bb2:
1439/// %tmp2 = tail call i32 @f2()
1440/// br label %return
1441/// return:
1442/// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
1443/// ret i32 %retval
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001444/// @endcode
Evan Cheng0663f232011-03-21 01:19:09 +00001445///
1446/// =>
1447///
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001448/// @code
Evan Cheng0663f232011-03-21 01:19:09 +00001449/// bb0:
1450/// %tmp0 = tail call i32 @f0()
1451/// ret i32 %tmp0
1452/// bb1:
1453/// %tmp1 = tail call i32 @f1()
1454/// ret i32 %tmp1
1455/// bb2:
1456/// %tmp2 = tail call i32 @f2()
1457/// ret i32 %tmp2
Dmitri Gribenko2bc1d482012-09-13 12:34:29 +00001458/// @endcode
Benjamin Kramer455fa352012-11-23 19:17:06 +00001459bool CodeGenPrepare::DupRetToEnableTailCallOpts(BasicBlock *BB) {
Cameron Zwarich47e71752011-03-24 04:51:51 +00001460 if (!TLI)
1461 return false;
1462
Benjamin Kramer455fa352012-11-23 19:17:06 +00001463 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
1464 if (!RI)
1465 return false;
1466
Craig Topperc0196b12014-04-14 00:51:57 +00001467 PHINode *PN = nullptr;
1468 BitCastInst *BCI = nullptr;
Evan Cheng0663f232011-03-21 01:19:09 +00001469 Value *V = RI->getReturnValue();
Evan Cheng249716e2012-07-27 21:21:26 +00001470 if (V) {
1471 BCI = dyn_cast<BitCastInst>(V);
1472 if (BCI)
1473 V = BCI->getOperand(0);
1474
1475 PN = dyn_cast<PHINode>(V);
1476 if (!PN)
1477 return false;
1478 }
Evan Cheng0663f232011-03-21 01:19:09 +00001479
Cameron Zwarich4649f172011-03-24 04:52:10 +00001480 if (PN && PN->getParent() != BB)
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001481 return false;
Evan Cheng0663f232011-03-21 01:19:09 +00001482
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001483 // It's not safe to eliminate the sign / zero extension of the return value.
1484 // See llvm::isInTailCallPosition().
1485 const Function *F = BB->getParent();
Bill Wendling658d24d2013-01-18 21:53:16 +00001486 AttributeSet CallerAttrs = F->getAttributes();
1487 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
1488 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001489 return false;
Evan Cheng0663f232011-03-21 01:19:09 +00001490
Cameron Zwarich4649f172011-03-24 04:52:10 +00001491 // Make sure there are no instructions between the PHI and return, or that the
1492 // return is the first instruction in the block.
1493 if (PN) {
1494 BasicBlock::iterator BI = BB->begin();
1495 do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
Evan Cheng249716e2012-07-27 21:21:26 +00001496 if (&*BI == BCI)
1497 // Also skip over the bitcast.
1498 ++BI;
Cameron Zwarich4649f172011-03-24 04:52:10 +00001499 if (&*BI != RI)
1500 return false;
1501 } else {
Cameron Zwarich74157ab2011-03-24 16:34:59 +00001502 BasicBlock::iterator BI = BB->begin();
1503 while (isa<DbgInfoIntrinsic>(BI)) ++BI;
1504 if (&*BI != RI)
Cameron Zwarich4649f172011-03-24 04:52:10 +00001505 return false;
1506 }
Evan Cheng0663f232011-03-21 01:19:09 +00001507
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001508 /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
1509 /// call.
1510 SmallVector<CallInst*, 4> TailCalls;
Cameron Zwarich4649f172011-03-24 04:52:10 +00001511 if (PN) {
1512 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
1513 CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
1514 // Make sure the phi value is indeed produced by the tail call.
1515 if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
1516 TLI->mayBeEmittedAsTailCall(CI))
1517 TailCalls.push_back(CI);
1518 }
1519 } else {
1520 SmallPtrSet<BasicBlock*, 4> VisitedBBs;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001521 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
David Blaikie70573dc2014-11-19 07:49:26 +00001522 if (!VisitedBBs.insert(*PI).second)
Cameron Zwarich4649f172011-03-24 04:52:10 +00001523 continue;
1524
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +00001525 BasicBlock::InstListType &InstList = (*PI)->getInstList();
Cameron Zwarich4649f172011-03-24 04:52:10 +00001526 BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
1527 BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
Cameron Zwarich74157ab2011-03-24 16:34:59 +00001528 do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
1529 if (RI == RE)
Cameron Zwarich4649f172011-03-24 04:52:10 +00001530 continue;
Cameron Zwarich74157ab2011-03-24 16:34:59 +00001531
Cameron Zwarich4649f172011-03-24 04:52:10 +00001532 CallInst *CI = dyn_cast<CallInst>(&*RI);
Cameron Zwarich2edfe772011-03-24 15:54:11 +00001533 if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI))
Cameron Zwarich4649f172011-03-24 04:52:10 +00001534 TailCalls.push_back(CI);
1535 }
Evan Cheng0663f232011-03-21 01:19:09 +00001536 }
1537
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001538 bool Changed = false;
1539 for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
1540 CallInst *CI = TailCalls[i];
1541 CallSite CS(CI);
1542
1543 // Conservatively require the attributes of the call to match those of the
1544 // return. Ignore noalias because it doesn't affect the call sequence.
Bill Wendling658d24d2013-01-18 21:53:16 +00001545 AttributeSet CalleeAttrs = CS.getAttributes();
1546 if (AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex).
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001547 removeAttribute(Attribute::NoAlias) !=
Bill Wendling658d24d2013-01-18 21:53:16 +00001548 AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex).
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001549 removeAttribute(Attribute::NoAlias))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001550 continue;
1551
1552 // Make sure the call instruction is followed by an unconditional branch to
1553 // the return block.
1554 BasicBlock *CallBB = CI->getParent();
1555 BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
1556 if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
1557 continue;
1558
1559 // Duplicate the return into CallBB.
1560 (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
Devang Patel8f606d72011-03-24 15:35:25 +00001561 ModifiedDT = Changed = true;
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001562 ++NumRetsDup;
1563 }
1564
1565 // If we eliminated all predecessors of the block, delete the block now.
Evan Cheng64a223a2012-09-28 23:58:57 +00001566 if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB))
Cameron Zwarich0e331c02011-03-24 04:52:07 +00001567 BB->eraseFromParent();
1568
1569 return Changed;
Evan Cheng0663f232011-03-21 01:19:09 +00001570}
1571
Chris Lattner728f9022008-11-25 07:09:13 +00001572//===----------------------------------------------------------------------===//
Chris Lattner728f9022008-11-25 07:09:13 +00001573// Memory Optimization
1574//===----------------------------------------------------------------------===//
1575
Chandler Carruthc8925912013-01-05 02:09:22 +00001576namespace {
1577
1578/// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
1579/// which holds actual Value*'s for register values.
Chandler Carruth95f83e02013-01-07 15:14:13 +00001580struct ExtAddrMode : public TargetLowering::AddrMode {
Chandler Carruthc8925912013-01-05 02:09:22 +00001581 Value *BaseReg;
1582 Value *ScaledReg;
Craig Topperc0196b12014-04-14 00:51:57 +00001583 ExtAddrMode() : BaseReg(nullptr), ScaledReg(nullptr) {}
Chandler Carruthc8925912013-01-05 02:09:22 +00001584 void print(raw_ostream &OS) const;
1585 void dump() const;
Stephen Lin837bba12013-07-15 17:55:02 +00001586
Chandler Carruthc8925912013-01-05 02:09:22 +00001587 bool operator==(const ExtAddrMode& O) const {
1588 return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) &&
1589 (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) &&
1590 (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale);
1591 }
1592};
1593
Eli Friedmanc1f1f852013-09-10 23:09:24 +00001594#ifndef NDEBUG
1595static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
1596 AM.print(OS);
1597 return OS;
1598}
1599#endif
1600
Chandler Carruthc8925912013-01-05 02:09:22 +00001601void ExtAddrMode::print(raw_ostream &OS) const {
1602 bool NeedPlus = false;
1603 OS << "[";
1604 if (BaseGV) {
1605 OS << (NeedPlus ? " + " : "")
1606 << "GV:";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001607 BaseGV->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00001608 NeedPlus = true;
1609 }
1610
Richard Trieuc0f91212014-05-30 03:15:17 +00001611 if (BaseOffs) {
1612 OS << (NeedPlus ? " + " : "")
1613 << BaseOffs;
1614 NeedPlus = true;
1615 }
Chandler Carruthc8925912013-01-05 02:09:22 +00001616
1617 if (BaseReg) {
1618 OS << (NeedPlus ? " + " : "")
1619 << "Base:";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001620 BaseReg->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00001621 NeedPlus = true;
1622 }
1623 if (Scale) {
1624 OS << (NeedPlus ? " + " : "")
1625 << Scale << "*";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001626 ScaledReg->printAsOperand(OS, /*PrintType=*/false);
Chandler Carruthc8925912013-01-05 02:09:22 +00001627 }
1628
1629 OS << ']';
1630}
1631
1632#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1633void ExtAddrMode::dump() const {
1634 print(dbgs());
1635 dbgs() << '\n';
1636}
1637#endif
1638
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001639/// \brief This class provides transaction based operation on the IR.
1640/// Every change made through this class is recorded in the internal state and
1641/// can be undone (rollback) until commit is called.
1642class TypePromotionTransaction {
1643
1644 /// \brief This represents the common interface of the individual transaction.
1645 /// Each class implements the logic for doing one specific modification on
1646 /// the IR via the TypePromotionTransaction.
1647 class TypePromotionAction {
1648 protected:
1649 /// The Instruction modified.
1650 Instruction *Inst;
1651
1652 public:
1653 /// \brief Constructor of the action.
1654 /// The constructor performs the related action on the IR.
1655 TypePromotionAction(Instruction *Inst) : Inst(Inst) {}
1656
1657 virtual ~TypePromotionAction() {}
1658
1659 /// \brief Undo the modification done by this action.
1660 /// When this method is called, the IR must be in the same state as it was
1661 /// before this action was applied.
1662 /// \pre Undoing the action works if and only if the IR is in the exact same
1663 /// state as it was directly after this action was applied.
1664 virtual void undo() = 0;
1665
1666 /// \brief Advocate every change made by this action.
1667 /// When the results on the IR of the action are to be kept, it is important
1668 /// to call this function, otherwise hidden information may be kept forever.
1669 virtual void commit() {
1670 // Nothing to be done, this action is not doing anything.
1671 }
1672 };
1673
1674 /// \brief Utility to remember the position of an instruction.
1675 class InsertionHandler {
1676 /// Position of an instruction.
1677 /// Either an instruction:
1678 /// - Is the first in a basic block: BB is used.
1679 /// - Has a previous instructon: PrevInst is used.
1680 union {
1681 Instruction *PrevInst;
1682 BasicBlock *BB;
1683 } Point;
1684 /// Remember whether or not the instruction had a previous instruction.
1685 bool HasPrevInstruction;
1686
1687 public:
1688 /// \brief Record the position of \p Inst.
1689 InsertionHandler(Instruction *Inst) {
1690 BasicBlock::iterator It = Inst;
1691 HasPrevInstruction = (It != (Inst->getParent()->begin()));
1692 if (HasPrevInstruction)
1693 Point.PrevInst = --It;
1694 else
1695 Point.BB = Inst->getParent();
1696 }
1697
1698 /// \brief Insert \p Inst at the recorded position.
1699 void insert(Instruction *Inst) {
1700 if (HasPrevInstruction) {
1701 if (Inst->getParent())
1702 Inst->removeFromParent();
1703 Inst->insertAfter(Point.PrevInst);
1704 } else {
1705 Instruction *Position = Point.BB->getFirstInsertionPt();
1706 if (Inst->getParent())
1707 Inst->moveBefore(Position);
1708 else
1709 Inst->insertBefore(Position);
1710 }
1711 }
1712 };
1713
1714 /// \brief Move an instruction before another.
1715 class InstructionMoveBefore : public TypePromotionAction {
1716 /// Original position of the instruction.
1717 InsertionHandler Position;
1718
1719 public:
1720 /// \brief Move \p Inst before \p Before.
1721 InstructionMoveBefore(Instruction *Inst, Instruction *Before)
1722 : TypePromotionAction(Inst), Position(Inst) {
1723 DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before << "\n");
1724 Inst->moveBefore(Before);
1725 }
1726
1727 /// \brief Move the instruction back to its original position.
Craig Topper4584cd52014-03-07 09:26:03 +00001728 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001729 DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n");
1730 Position.insert(Inst);
1731 }
1732 };
1733
1734 /// \brief Set the operand of an instruction with a new value.
1735 class OperandSetter : public TypePromotionAction {
1736 /// Original operand of the instruction.
1737 Value *Origin;
1738 /// Index of the modified instruction.
1739 unsigned Idx;
1740
1741 public:
1742 /// \brief Set \p Idx operand of \p Inst with \p NewVal.
1743 OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal)
1744 : TypePromotionAction(Inst), Idx(Idx) {
1745 DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n"
1746 << "for:" << *Inst << "\n"
1747 << "with:" << *NewVal << "\n");
1748 Origin = Inst->getOperand(Idx);
1749 Inst->setOperand(Idx, NewVal);
1750 }
1751
1752 /// \brief Restore the original value of the instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00001753 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001754 DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n"
1755 << "for: " << *Inst << "\n"
1756 << "with: " << *Origin << "\n");
1757 Inst->setOperand(Idx, Origin);
1758 }
1759 };
1760
1761 /// \brief Hide the operands of an instruction.
1762 /// Do as if this instruction was not using any of its operands.
1763 class OperandsHider : public TypePromotionAction {
1764 /// The list of original operands.
1765 SmallVector<Value *, 4> OriginalValues;
1766
1767 public:
1768 /// \brief Remove \p Inst from the uses of the operands of \p Inst.
1769 OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) {
1770 DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n");
1771 unsigned NumOpnds = Inst->getNumOperands();
1772 OriginalValues.reserve(NumOpnds);
1773 for (unsigned It = 0; It < NumOpnds; ++It) {
1774 // Save the current operand.
1775 Value *Val = Inst->getOperand(It);
1776 OriginalValues.push_back(Val);
1777 // Set a dummy one.
1778 // We could use OperandSetter here, but that would implied an overhead
1779 // that we are not willing to pay.
1780 Inst->setOperand(It, UndefValue::get(Val->getType()));
1781 }
1782 }
1783
1784 /// \brief Restore the original list of uses.
Craig Topper4584cd52014-03-07 09:26:03 +00001785 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001786 DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n");
1787 for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It)
1788 Inst->setOperand(It, OriginalValues[It]);
1789 }
1790 };
1791
1792 /// \brief Build a truncate instruction.
1793 class TruncBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00001794 Value *Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001795 public:
1796 /// \brief Build a truncate instruction of \p Opnd producing a \p Ty
1797 /// result.
1798 /// trunc Opnd to Ty.
1799 TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) {
1800 IRBuilder<> Builder(Opnd);
Quentin Colombetac55b152014-09-16 22:36:07 +00001801 Val = Builder.CreateTrunc(Opnd, Ty, "promoted");
1802 DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n");
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001803 }
1804
Quentin Colombetac55b152014-09-16 22:36:07 +00001805 /// \brief Get the built value.
1806 Value *getBuiltValue() { return Val; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001807
1808 /// \brief Remove the built instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00001809 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00001810 DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n");
1811 if (Instruction *IVal = dyn_cast<Instruction>(Val))
1812 IVal->eraseFromParent();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001813 }
1814 };
1815
1816 /// \brief Build a sign extension instruction.
1817 class SExtBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00001818 Value *Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001819 public:
1820 /// \brief Build a sign extension instruction of \p Opnd producing a \p Ty
1821 /// result.
1822 /// sext Opnd to Ty.
1823 SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
Quentin Colombetac55b152014-09-16 22:36:07 +00001824 : TypePromotionAction(InsertPt) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001825 IRBuilder<> Builder(InsertPt);
Quentin Colombetac55b152014-09-16 22:36:07 +00001826 Val = Builder.CreateSExt(Opnd, Ty, "promoted");
1827 DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n");
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001828 }
1829
Quentin Colombetac55b152014-09-16 22:36:07 +00001830 /// \brief Get the built value.
1831 Value *getBuiltValue() { return Val; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001832
1833 /// \brief Remove the built instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00001834 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00001835 DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n");
1836 if (Instruction *IVal = dyn_cast<Instruction>(Val))
1837 IVal->eraseFromParent();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001838 }
1839 };
1840
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001841 /// \brief Build a zero extension instruction.
1842 class ZExtBuilder : public TypePromotionAction {
Quentin Colombetac55b152014-09-16 22:36:07 +00001843 Value *Val;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001844 public:
1845 /// \brief Build a zero extension instruction of \p Opnd producing a \p Ty
1846 /// result.
1847 /// zext Opnd to Ty.
1848 ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
Quentin Colombetac55b152014-09-16 22:36:07 +00001849 : TypePromotionAction(InsertPt) {
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001850 IRBuilder<> Builder(InsertPt);
Quentin Colombetac55b152014-09-16 22:36:07 +00001851 Val = Builder.CreateZExt(Opnd, Ty, "promoted");
1852 DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n");
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001853 }
1854
Quentin Colombetac55b152014-09-16 22:36:07 +00001855 /// \brief Get the built value.
1856 Value *getBuiltValue() { return Val; }
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001857
1858 /// \brief Remove the built instruction.
1859 void undo() override {
Quentin Colombetac55b152014-09-16 22:36:07 +00001860 DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n");
1861 if (Instruction *IVal = dyn_cast<Instruction>(Val))
1862 IVal->eraseFromParent();
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001863 }
1864 };
1865
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001866 /// \brief Mutate an instruction to another type.
1867 class TypeMutator : public TypePromotionAction {
1868 /// Record the original type.
1869 Type *OrigTy;
1870
1871 public:
1872 /// \brief Mutate the type of \p Inst into \p NewTy.
1873 TypeMutator(Instruction *Inst, Type *NewTy)
1874 : TypePromotionAction(Inst), OrigTy(Inst->getType()) {
1875 DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy
1876 << "\n");
1877 Inst->mutateType(NewTy);
1878 }
1879
1880 /// \brief Mutate the instruction back to its original type.
Craig Topper4584cd52014-03-07 09:26:03 +00001881 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001882 DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy
1883 << "\n");
1884 Inst->mutateType(OrigTy);
1885 }
1886 };
1887
1888 /// \brief Replace the uses of an instruction by another instruction.
1889 class UsesReplacer : public TypePromotionAction {
1890 /// Helper structure to keep track of the replaced uses.
1891 struct InstructionAndIdx {
1892 /// The instruction using the instruction.
1893 Instruction *Inst;
1894 /// The index where this instruction is used for Inst.
1895 unsigned Idx;
1896 InstructionAndIdx(Instruction *Inst, unsigned Idx)
1897 : Inst(Inst), Idx(Idx) {}
1898 };
1899
1900 /// Keep track of the original uses (pair Instruction, Index).
1901 SmallVector<InstructionAndIdx, 4> OriginalUses;
1902 typedef SmallVectorImpl<InstructionAndIdx>::iterator use_iterator;
1903
1904 public:
1905 /// \brief Replace all the use of \p Inst by \p New.
1906 UsesReplacer(Instruction *Inst, Value *New) : TypePromotionAction(Inst) {
1907 DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New
1908 << "\n");
1909 // Record the original uses.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001910 for (Use &U : Inst->uses()) {
1911 Instruction *UserI = cast<Instruction>(U.getUser());
1912 OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo()));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001913 }
1914 // Now, we can replace the uses.
1915 Inst->replaceAllUsesWith(New);
1916 }
1917
1918 /// \brief Reassign the original uses of Inst to Inst.
Craig Topper4584cd52014-03-07 09:26:03 +00001919 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001920 DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n");
1921 for (use_iterator UseIt = OriginalUses.begin(),
1922 EndIt = OriginalUses.end();
1923 UseIt != EndIt; ++UseIt) {
1924 UseIt->Inst->setOperand(UseIt->Idx, Inst);
1925 }
1926 }
1927 };
1928
1929 /// \brief Remove an instruction from the IR.
1930 class InstructionRemover : public TypePromotionAction {
1931 /// Original position of the instruction.
1932 InsertionHandler Inserter;
1933 /// Helper structure to hide all the link to the instruction. In other
1934 /// words, this helps to do as if the instruction was removed.
1935 OperandsHider Hider;
1936 /// Keep track of the uses replaced, if any.
1937 UsesReplacer *Replacer;
1938
1939 public:
1940 /// \brief Remove all reference of \p Inst and optinally replace all its
1941 /// uses with New.
Craig Topperc0196b12014-04-14 00:51:57 +00001942 /// \pre If !Inst->use_empty(), then New != nullptr
1943 InstructionRemover(Instruction *Inst, Value *New = nullptr)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001944 : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst),
Craig Topperc0196b12014-04-14 00:51:57 +00001945 Replacer(nullptr) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001946 if (New)
1947 Replacer = new UsesReplacer(Inst, New);
1948 DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n");
1949 Inst->removeFromParent();
1950 }
1951
Alexander Kornienkof817c1c2015-04-11 02:11:45 +00001952 ~InstructionRemover() override { delete Replacer; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001953
1954 /// \brief Really remove the instruction.
Craig Topper4584cd52014-03-07 09:26:03 +00001955 void commit() override { delete Inst; }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001956
1957 /// \brief Resurrect the instruction and reassign it to the proper uses if
1958 /// new value was provided when build this action.
Craig Topper4584cd52014-03-07 09:26:03 +00001959 void undo() override {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001960 DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n");
1961 Inserter.insert(Inst);
1962 if (Replacer)
1963 Replacer->undo();
1964 Hider.undo();
1965 }
1966 };
1967
1968public:
1969 /// Restoration point.
1970 /// The restoration point is a pointer to an action instead of an iterator
1971 /// because the iterator may be invalidated but not the pointer.
1972 typedef const TypePromotionAction *ConstRestorationPt;
1973 /// Advocate every changes made in that transaction.
1974 void commit();
1975 /// Undo all the changes made after the given point.
1976 void rollback(ConstRestorationPt Point);
1977 /// Get the current restoration point.
1978 ConstRestorationPt getRestorationPoint() const;
1979
1980 /// \name API for IR modification with state keeping to support rollback.
1981 /// @{
1982 /// Same as Instruction::setOperand.
1983 void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal);
1984 /// Same as Instruction::eraseFromParent.
Craig Topperc0196b12014-04-14 00:51:57 +00001985 void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001986 /// Same as Value::replaceAllUsesWith.
1987 void replaceAllUsesWith(Instruction *Inst, Value *New);
1988 /// Same as Value::mutateType.
1989 void mutateType(Instruction *Inst, Type *NewTy);
1990 /// Same as IRBuilder::createTrunc.
Quentin Colombetac55b152014-09-16 22:36:07 +00001991 Value *createTrunc(Instruction *Opnd, Type *Ty);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001992 /// Same as IRBuilder::createSExt.
Quentin Colombetac55b152014-09-16 22:36:07 +00001993 Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty);
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00001994 /// Same as IRBuilder::createZExt.
Quentin Colombetac55b152014-09-16 22:36:07 +00001995 Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00001996 /// Same as Instruction::moveBefore.
1997 void moveBefore(Instruction *Inst, Instruction *Before);
1998 /// @}
1999
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002000private:
2001 /// The ordered list of actions made so far.
David Blaikie7620b312014-04-15 06:17:44 +00002002 SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions;
2003 typedef SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator CommitPt;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002004};
2005
2006void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx,
2007 Value *NewVal) {
2008 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002009 make_unique<TypePromotionTransaction::OperandSetter>(Inst, Idx, NewVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002010}
2011
2012void TypePromotionTransaction::eraseInstruction(Instruction *Inst,
2013 Value *NewVal) {
2014 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002015 make_unique<TypePromotionTransaction::InstructionRemover>(Inst, NewVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002016}
2017
2018void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst,
2019 Value *New) {
David Blaikie7620b312014-04-15 06:17:44 +00002020 Actions.push_back(make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002021}
2022
2023void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) {
David Blaikie7620b312014-04-15 06:17:44 +00002024 Actions.push_back(make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002025}
2026
Quentin Colombetac55b152014-09-16 22:36:07 +00002027Value *TypePromotionTransaction::createTrunc(Instruction *Opnd,
2028 Type *Ty) {
David Blaikie7620b312014-04-15 06:17:44 +00002029 std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002030 Value *Val = Ptr->getBuiltValue();
David Blaikie7620b312014-04-15 06:17:44 +00002031 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002032 return Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002033}
2034
Quentin Colombetac55b152014-09-16 22:36:07 +00002035Value *TypePromotionTransaction::createSExt(Instruction *Inst,
2036 Value *Opnd, Type *Ty) {
David Blaikie7620b312014-04-15 06:17:44 +00002037 std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002038 Value *Val = Ptr->getBuiltValue();
David Blaikie7620b312014-04-15 06:17:44 +00002039 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002040 return Val;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002041}
2042
Quentin Colombetac55b152014-09-16 22:36:07 +00002043Value *TypePromotionTransaction::createZExt(Instruction *Inst,
2044 Value *Opnd, Type *Ty) {
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002045 std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty));
Quentin Colombetac55b152014-09-16 22:36:07 +00002046 Value *Val = Ptr->getBuiltValue();
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002047 Actions.push_back(std::move(Ptr));
Quentin Colombetac55b152014-09-16 22:36:07 +00002048 return Val;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002049}
2050
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002051void TypePromotionTransaction::moveBefore(Instruction *Inst,
2052 Instruction *Before) {
2053 Actions.push_back(
David Blaikie7620b312014-04-15 06:17:44 +00002054 make_unique<TypePromotionTransaction::InstructionMoveBefore>(Inst, Before));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002055}
2056
2057TypePromotionTransaction::ConstRestorationPt
2058TypePromotionTransaction::getRestorationPoint() const {
David Blaikie7620b312014-04-15 06:17:44 +00002059 return !Actions.empty() ? Actions.back().get() : nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002060}
2061
2062void TypePromotionTransaction::commit() {
2063 for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt;
David Blaikie7620b312014-04-15 06:17:44 +00002064 ++It)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002065 (*It)->commit();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002066 Actions.clear();
2067}
2068
2069void TypePromotionTransaction::rollback(
2070 TypePromotionTransaction::ConstRestorationPt Point) {
David Blaikie7620b312014-04-15 06:17:44 +00002071 while (!Actions.empty() && Point != Actions.back().get()) {
2072 std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002073 Curr->undo();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002074 }
2075}
2076
Chandler Carruthc8925912013-01-05 02:09:22 +00002077/// \brief A helper class for matching addressing modes.
2078///
2079/// This encapsulates the logic for matching the target-legal addressing modes.
2080class AddressingModeMatcher {
2081 SmallVectorImpl<Instruction*> &AddrModeInsts;
Eric Christopherd75c00c2015-02-26 22:38:34 +00002082 const TargetMachine &TM;
Chandler Carruthc8925912013-01-05 02:09:22 +00002083 const TargetLowering &TLI;
2084
2085 /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
2086 /// the memory instruction that we're computing this address for.
2087 Type *AccessTy;
2088 Instruction *MemoryInst;
Stephen Lin837bba12013-07-15 17:55:02 +00002089
Chandler Carruthc8925912013-01-05 02:09:22 +00002090 /// AddrMode - This is the addressing mode that we're building up. This is
2091 /// part of the return value of this addressing mode matching stuff.
2092 ExtAddrMode &AddrMode;
Stephen Lin837bba12013-07-15 17:55:02 +00002093
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002094 /// The truncate instruction inserted by other CodeGenPrepare optimizations.
2095 const SetOfInstrs &InsertedTruncs;
2096 /// A map from the instructions to their type before promotion.
2097 InstrToOrigTy &PromotedInsts;
2098 /// The ongoing transaction where every action should be registered.
2099 TypePromotionTransaction &TPT;
2100
Chandler Carruthc8925912013-01-05 02:09:22 +00002101 /// IgnoreProfitability - This is set to true when we should not do
2102 /// profitability checks. When true, IsProfitableToFoldIntoAddressingMode
2103 /// always returns true.
2104 bool IgnoreProfitability;
Stephen Lin837bba12013-07-15 17:55:02 +00002105
Eric Christopherd75c00c2015-02-26 22:38:34 +00002106 AddressingModeMatcher(SmallVectorImpl<Instruction *> &AMI,
2107 const TargetMachine &TM, Type *AT, Instruction *MI,
2108 ExtAddrMode &AM, const SetOfInstrs &InsertedTruncs,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002109 InstrToOrigTy &PromotedInsts,
2110 TypePromotionTransaction &TPT)
Eric Christopherd75c00c2015-02-26 22:38:34 +00002111 : AddrModeInsts(AMI), TM(TM),
2112 TLI(*TM.getSubtargetImpl(*MI->getParent()->getParent())
2113 ->getTargetLowering()),
2114 AccessTy(AT), MemoryInst(MI), AddrMode(AM),
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002115 InsertedTruncs(InsertedTruncs), PromotedInsts(PromotedInsts), TPT(TPT) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002116 IgnoreProfitability = false;
2117 }
2118public:
Stephen Lin837bba12013-07-15 17:55:02 +00002119
Chandler Carruthc8925912013-01-05 02:09:22 +00002120 /// Match - Find the maximal addressing mode that a load/store of V can fold,
2121 /// give an access type of AccessTy. This returns a list of involved
2122 /// instructions in AddrModeInsts.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002123 /// \p InsertedTruncs The truncate instruction inserted by other
2124 /// CodeGenPrepare
2125 /// optimizations.
2126 /// \p PromotedInsts maps the instructions to their type before promotion.
2127 /// \p The ongoing transaction where every action should be registered.
Chandler Carruthc8925912013-01-05 02:09:22 +00002128 static ExtAddrMode Match(Value *V, Type *AccessTy,
2129 Instruction *MemoryInst,
2130 SmallVectorImpl<Instruction*> &AddrModeInsts,
Eric Christopherd75c00c2015-02-26 22:38:34 +00002131 const TargetMachine &TM,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002132 const SetOfInstrs &InsertedTruncs,
2133 InstrToOrigTy &PromotedInsts,
2134 TypePromotionTransaction &TPT) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002135 ExtAddrMode Result;
2136
Eric Christopherd75c00c2015-02-26 22:38:34 +00002137 bool Success = AddressingModeMatcher(AddrModeInsts, TM, AccessTy,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002138 MemoryInst, Result, InsertedTruncs,
2139 PromotedInsts, TPT).MatchAddr(V, 0);
Chandler Carruthc8925912013-01-05 02:09:22 +00002140 (void)Success; assert(Success && "Couldn't select *anything*?");
2141 return Result;
2142 }
2143private:
2144 bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
2145 bool MatchAddr(Value *V, unsigned Depth);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002146 bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth,
Craig Topperc0196b12014-04-14 00:51:57 +00002147 bool *MovedAway = nullptr);
Chandler Carruthc8925912013-01-05 02:09:22 +00002148 bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
2149 ExtAddrMode &AMBefore,
2150 ExtAddrMode &AMAfter);
2151 bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
Quentin Colombet1b274f92015-03-10 21:48:15 +00002152 bool IsPromotionProfitable(unsigned NewCost, unsigned OldCost,
Quentin Colombet867c5502014-02-14 22:23:22 +00002153 Value *PromotedOperand) const;
Chandler Carruthc8925912013-01-05 02:09:22 +00002154};
2155
2156/// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode.
2157/// Return true and update AddrMode if this addr mode is legal for the target,
2158/// false if not.
2159bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale,
2160 unsigned Depth) {
2161 // If Scale is 1, then this is the same as adding ScaleReg to the addressing
2162 // mode. Just process that directly.
2163 if (Scale == 1)
2164 return MatchAddr(ScaleReg, Depth);
Stephen Lin837bba12013-07-15 17:55:02 +00002165
Chandler Carruthc8925912013-01-05 02:09:22 +00002166 // If the scale is 0, it takes nothing to add this.
2167 if (Scale == 0)
2168 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00002169
Chandler Carruthc8925912013-01-05 02:09:22 +00002170 // If we already have a scale of this value, we can add to it, otherwise, we
2171 // need an available scale field.
2172 if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
2173 return false;
2174
2175 ExtAddrMode TestAddrMode = AddrMode;
2176
2177 // Add scale to turn X*4+X*3 -> X*7. This could also do things like
2178 // [A+B + A*7] -> [B+A*8].
2179 TestAddrMode.Scale += Scale;
2180 TestAddrMode.ScaledReg = ScaleReg;
2181
2182 // If the new address isn't legal, bail out.
2183 if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy))
2184 return false;
2185
2186 // It was legal, so commit it.
2187 AddrMode = TestAddrMode;
Stephen Lin837bba12013-07-15 17:55:02 +00002188
Chandler Carruthc8925912013-01-05 02:09:22 +00002189 // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now
2190 // to see if ScaleReg is actually X+C. If so, we can turn this into adding
2191 // X*Scale + C*Scale to addr mode.
Craig Topperc0196b12014-04-14 00:51:57 +00002192 ConstantInt *CI = nullptr; Value *AddLHS = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00002193 if (isa<Instruction>(ScaleReg) && // not a constant expr.
2194 match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) {
2195 TestAddrMode.ScaledReg = AddLHS;
2196 TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
Stephen Lin837bba12013-07-15 17:55:02 +00002197
Chandler Carruthc8925912013-01-05 02:09:22 +00002198 // If this addressing mode is legal, commit it and remember that we folded
2199 // this instruction.
2200 if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) {
2201 AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
2202 AddrMode = TestAddrMode;
2203 return true;
2204 }
2205 }
2206
2207 // Otherwise, not (x+c)*scale, just return what we have.
2208 return true;
2209}
2210
2211/// MightBeFoldableInst - This is a little filter, which returns true if an
2212/// addressing computation involving I might be folded into a load/store
2213/// accessing it. This doesn't need to be perfect, but needs to accept at least
2214/// the set of instructions that MatchOperationAddr can.
2215static bool MightBeFoldableInst(Instruction *I) {
2216 switch (I->getOpcode()) {
2217 case Instruction::BitCast:
Eli Benderskyf13a0562014-05-22 00:02:52 +00002218 case Instruction::AddrSpaceCast:
Chandler Carruthc8925912013-01-05 02:09:22 +00002219 // Don't touch identity bitcasts.
2220 if (I->getType() == I->getOperand(0)->getType())
2221 return false;
2222 return I->getType()->isPointerTy() || I->getType()->isIntegerTy();
2223 case Instruction::PtrToInt:
2224 // PtrToInt is always a noop, as we know that the int type is pointer sized.
2225 return true;
2226 case Instruction::IntToPtr:
2227 // We know the input is intptr_t, so this is foldable.
2228 return true;
2229 case Instruction::Add:
2230 return true;
2231 case Instruction::Mul:
2232 case Instruction::Shl:
2233 // Can only handle X*C and X << C.
2234 return isa<ConstantInt>(I->getOperand(1));
2235 case Instruction::GetElementPtr:
2236 return true;
2237 default:
2238 return false;
2239 }
2240}
2241
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002242/// \brief Check whether or not \p Val is a legal instruction for \p TLI.
2243/// \note \p Val is assumed to be the product of some type promotion.
2244/// Therefore if \p Val has an undefined state in \p TLI, this is assumed
2245/// to be legal, as the non-promoted value would have had the same state.
2246static bool isPromotedInstructionLegal(const TargetLowering &TLI, Value *Val) {
2247 Instruction *PromotedInst = dyn_cast<Instruction>(Val);
2248 if (!PromotedInst)
2249 return false;
2250 int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode());
2251 // If the ISDOpcode is undefined, it was undefined before the promotion.
2252 if (!ISDOpcode)
2253 return true;
2254 // Otherwise, check if the promoted instruction is legal or not.
2255 return TLI.isOperationLegalOrCustom(
2256 ISDOpcode, TLI.getValueType(PromotedInst->getType()));
2257}
2258
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002259/// \brief Hepler class to perform type promotion.
2260class TypePromotionHelper {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002261 /// \brief Utility function to check whether or not a sign or zero extension
2262 /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by
2263 /// either using the operands of \p Inst or promoting \p Inst.
2264 /// The type of the extension is defined by \p IsSExt.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002265 /// In other words, check if:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002266 /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002267 /// #1 Promotion applies:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002268 /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...).
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002269 /// #2 Operand reuses:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002270 /// ext opnd1 to ConsideredExtType.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002271 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002272 static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType,
2273 const InstrToOrigTy &PromotedInsts, bool IsSExt);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002274
2275 /// \brief Utility function to determine if \p OpIdx should be promoted when
2276 /// promoting \p Inst.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002277 static bool shouldExtOperand(const Instruction *Inst, int OpIdx) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002278 if (isa<SelectInst>(Inst) && OpIdx == 0)
2279 return false;
2280 return true;
2281 }
2282
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002283 /// \brief Utility function to promote the operand of \p Ext when this
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002284 /// operand is a promotable trunc or sext or zext.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002285 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002286 /// \p CreatedInstsCost[out] contains the cost of all instructions
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002287 /// created to promote the operand of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002288 /// Newly added extensions are inserted in \p Exts.
2289 /// Newly added truncates are inserted in \p Truncs.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002290 /// Should never be called directly.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002291 /// \return The promoted value which is used instead of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002292 static Value *promoteOperandForTruncAndAnyExt(
2293 Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002294 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002295 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002296 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002297
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002298 /// \brief Utility function to promote the operand of \p Ext when this
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002299 /// operand is promotable and is not a supported trunc or sext.
2300 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002301 /// \p CreatedInstsCost[out] contains the cost of all the instructions
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002302 /// created to promote the operand of Ext.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002303 /// Newly added extensions are inserted in \p Exts.
2304 /// Newly added truncates are inserted in \p Truncs.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002305 /// Should never be called directly.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002306 /// \return The promoted value which is used instead of Ext.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002307 static Value *promoteOperandForOther(Instruction *Ext,
2308 TypePromotionTransaction &TPT,
2309 InstrToOrigTy &PromotedInsts,
2310 unsigned &CreatedInstsCost,
2311 SmallVectorImpl<Instruction *> *Exts,
2312 SmallVectorImpl<Instruction *> *Truncs,
2313 const TargetLowering &TLI, bool IsSExt);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002314
2315 /// \see promoteOperandForOther.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002316 static Value *signExtendOperandForOther(
2317 Instruction *Ext, TypePromotionTransaction &TPT,
2318 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
2319 SmallVectorImpl<Instruction *> *Exts,
2320 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
2321 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
2322 Exts, Truncs, TLI, true);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002323 }
2324
2325 /// \see promoteOperandForOther.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002326 static Value *zeroExtendOperandForOther(
2327 Instruction *Ext, TypePromotionTransaction &TPT,
2328 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
2329 SmallVectorImpl<Instruction *> *Exts,
2330 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
2331 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
2332 Exts, Truncs, TLI, false);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002333 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002334
2335public:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002336 /// Type for the utility function that promotes the operand of Ext.
2337 typedef Value *(*Action)(Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002338 InstrToOrigTy &PromotedInsts,
2339 unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002340 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002341 SmallVectorImpl<Instruction *> *Truncs,
2342 const TargetLowering &TLI);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002343 /// \brief Given a sign/zero extend instruction \p Ext, return the approriate
2344 /// action to promote the operand of \p Ext instead of using Ext.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002345 /// \return NULL if no promotable action is possible with the current
2346 /// sign extension.
2347 /// \p InsertedTruncs keeps track of all the truncate instructions inserted by
2348 /// the others CodeGenPrepare optimizations. This information is important
2349 /// because we do not want to promote these instructions as CodeGenPrepare
2350 /// will reinsert them later. Thus creating an infinite loop: create/remove.
2351 /// \p PromotedInsts maps the instructions to their type before promotion.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002352 static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedTruncs,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002353 const TargetLowering &TLI,
2354 const InstrToOrigTy &PromotedInsts);
2355};
2356
2357bool TypePromotionHelper::canGetThrough(const Instruction *Inst,
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002358 Type *ConsideredExtType,
2359 const InstrToOrigTy &PromotedInsts,
2360 bool IsSExt) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002361 // The promotion helper does not know how to deal with vector types yet.
2362 // To be able to fix that, we would need to fix the places where we
2363 // statically extend, e.g., constants and such.
2364 if (Inst->getType()->isVectorTy())
2365 return false;
2366
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002367 // We can always get through zext.
2368 if (isa<ZExtInst>(Inst))
2369 return true;
2370
2371 // sext(sext) is ok too.
2372 if (IsSExt && isa<SExtInst>(Inst))
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002373 return true;
2374
2375 // We can get through binary operator, if it is legal. In other words, the
2376 // binary operator must have a nuw or nsw flag.
2377 const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst);
2378 if (BinOp && isa<OverflowingBinaryOperator>(BinOp) &&
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002379 ((!IsSExt && BinOp->hasNoUnsignedWrap()) ||
2380 (IsSExt && BinOp->hasNoSignedWrap())))
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002381 return true;
2382
2383 // Check if we can do the following simplification.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002384 // ext(trunc(opnd)) --> ext(opnd)
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002385 if (!isa<TruncInst>(Inst))
2386 return false;
2387
2388 Value *OpndVal = Inst->getOperand(0);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002389 // Check if we can use this operand in the extension.
2390 // If the type is larger than the result type of the extension,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002391 // we cannot.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002392 if (!OpndVal->getType()->isIntegerTy() ||
2393 OpndVal->getType()->getIntegerBitWidth() >
2394 ConsideredExtType->getIntegerBitWidth())
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002395 return false;
2396
2397 // If the operand of the truncate is not an instruction, we will not have
2398 // any information on the dropped bits.
2399 // (Actually we could for constant but it is not worth the extra logic).
2400 Instruction *Opnd = dyn_cast<Instruction>(OpndVal);
2401 if (!Opnd)
2402 return false;
2403
2404 // Check if the source of the type is narrow enough.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002405 // I.e., check that trunc just drops extended bits of the same kind of
2406 // the extension.
2407 // #1 get the type of the operand and check the kind of the extended bits.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002408 const Type *OpndType;
2409 InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002410 if (It != PromotedInsts.end() && It->second.IsSExt == IsSExt)
2411 OpndType = It->second.Ty;
2412 else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd)))
2413 OpndType = Opnd->getOperand(0)->getType();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002414 else
2415 return false;
2416
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002417 // #2 check that the truncate just drop extended bits.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002418 if (Inst->getType()->getIntegerBitWidth() >= OpndType->getIntegerBitWidth())
2419 return true;
2420
2421 return false;
2422}
2423
2424TypePromotionHelper::Action TypePromotionHelper::getAction(
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002425 Instruction *Ext, const SetOfInstrs &InsertedTruncs,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002426 const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002427 assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
2428 "Unexpected instruction type");
2429 Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0));
2430 Type *ExtTy = Ext->getType();
2431 bool IsSExt = isa<SExtInst>(Ext);
2432 // If the operand of the extension is not an instruction, we cannot
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002433 // get through.
2434 // If it, check we can get through.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002435 if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt))
Craig Topperc0196b12014-04-14 00:51:57 +00002436 return nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002437
2438 // Do not promote if the operand has been added by codegenprepare.
2439 // Otherwise, it means we are undoing an optimization that is likely to be
2440 // redone, thus causing potential infinite loop.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002441 if (isa<TruncInst>(ExtOpnd) && InsertedTruncs.count(ExtOpnd))
Craig Topperc0196b12014-04-14 00:51:57 +00002442 return nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002443
2444 // SExt or Trunc instructions.
2445 // Return the related handler.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002446 if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) ||
2447 isa<ZExtInst>(ExtOpnd))
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002448 return promoteOperandForTruncAndAnyExt;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002449
2450 // Regular instruction.
2451 // Abort early if we will have to insert non-free instructions.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002452 if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType()))
Craig Topperc0196b12014-04-14 00:51:57 +00002453 return nullptr;
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002454 return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002455}
2456
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002457Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt(
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002458 llvm::Instruction *SExt, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002459 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002460 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002461 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002462 // By construction, the operand of SExt is an instruction. Otherwise we cannot
2463 // get through it and this method should not be called.
2464 Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0));
Quentin Colombetac55b152014-09-16 22:36:07 +00002465 Value *ExtVal = SExt;
Quentin Colombet1b274f92015-03-10 21:48:15 +00002466 bool HasMergedNonFreeExt = false;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002467 if (isa<ZExtInst>(SExtOpnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002468 // Replace s|zext(zext(opnd))
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002469 // => zext(opnd).
Quentin Colombet1b274f92015-03-10 21:48:15 +00002470 HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd);
Quentin Colombetac55b152014-09-16 22:36:07 +00002471 Value *ZExt =
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002472 TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType());
2473 TPT.replaceAllUsesWith(SExt, ZExt);
2474 TPT.eraseInstruction(SExt);
Quentin Colombetac55b152014-09-16 22:36:07 +00002475 ExtVal = ZExt;
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002476 } else {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002477 // Replace z|sext(trunc(opnd)) or sext(sext(opnd))
2478 // => z|sext(opnd).
Quentin Colombetb2c5c6d2014-09-11 21:22:14 +00002479 TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0));
2480 }
Quentin Colombet1b274f92015-03-10 21:48:15 +00002481 CreatedInstsCost = 0;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002482
2483 // Remove dead code.
2484 if (SExtOpnd->use_empty())
2485 TPT.eraseInstruction(SExtOpnd);
2486
Quentin Colombet9dcb7242014-09-15 18:26:58 +00002487 // Check if the extension is still needed.
Quentin Colombetac55b152014-09-16 22:36:07 +00002488 Instruction *ExtInst = dyn_cast<Instruction>(ExtVal);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002489 if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) {
Quentin Colombet1b274f92015-03-10 21:48:15 +00002490 if (ExtInst) {
2491 if (Exts)
2492 Exts->push_back(ExtInst);
2493 CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt;
2494 }
Quentin Colombetac55b152014-09-16 22:36:07 +00002495 return ExtVal;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002496 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002497
Quentin Colombet9dcb7242014-09-15 18:26:58 +00002498 // At this point we have: ext ty opnd to ty.
2499 // Reassign the uses of ExtInst to the opnd and remove ExtInst.
2500 Value *NextVal = ExtInst->getOperand(0);
2501 TPT.eraseInstruction(ExtInst, NextVal);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002502 return NextVal;
2503}
2504
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002505Value *TypePromotionHelper::promoteOperandForOther(
2506 Instruction *Ext, TypePromotionTransaction &TPT,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002507 InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002508 SmallVectorImpl<Instruction *> *Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00002509 SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI,
2510 bool IsSExt) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002511 // By construction, the operand of Ext is an instruction. Otherwise we cannot
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002512 // get through it and this method should not be called.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002513 Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0));
Quentin Colombet1b274f92015-03-10 21:48:15 +00002514 CreatedInstsCost = 0;
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002515 if (!ExtOpnd->hasOneUse()) {
2516 // ExtOpnd will be promoted.
2517 // All its uses, but Ext, will need to use a truncated value of the
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002518 // promoted version.
2519 // Create the truncate now.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002520 Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType());
Quentin Colombetac55b152014-09-16 22:36:07 +00002521 if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) {
2522 ITrunc->removeFromParent();
2523 // Insert it just after the definition.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002524 ITrunc->insertAfter(ExtOpnd);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002525 if (Truncs)
2526 Truncs->push_back(ITrunc);
Quentin Colombetac55b152014-09-16 22:36:07 +00002527 }
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002528
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002529 TPT.replaceAllUsesWith(ExtOpnd, Trunc);
2530 // Restore the operand of Ext (which has been replace by the previous call
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002531 // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002532 TPT.setOperand(Ext, 0, ExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002533 }
2534
2535 // Get through the Instruction:
2536 // 1. Update its type.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002537 // 2. Replace the uses of Ext by Inst.
2538 // 3. Extend each operand that needs to be extended.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002539
2540 // Remember the original type of the instruction before promotion.
2541 // This is useful to know that the high bits are sign extended bits.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002542 PromotedInsts.insert(std::pair<Instruction *, TypeIsSExt>(
2543 ExtOpnd, TypeIsSExt(ExtOpnd->getType(), IsSExt)));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002544 // Step #1.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002545 TPT.mutateType(ExtOpnd, Ext->getType());
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002546 // Step #2.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002547 TPT.replaceAllUsesWith(Ext, ExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002548 // Step #3.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002549 Instruction *ExtForOpnd = Ext;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002550
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002551 DEBUG(dbgs() << "Propagate Ext to operands\n");
2552 for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002553 ++OpIdx) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002554 DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n');
2555 if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() ||
2556 !shouldExtOperand(ExtOpnd, OpIdx)) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002557 DEBUG(dbgs() << "No need to propagate\n");
2558 continue;
2559 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002560 // Check if we can statically extend the operand.
2561 Value *Opnd = ExtOpnd->getOperand(OpIdx);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002562 if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002563 DEBUG(dbgs() << "Statically extend\n");
2564 unsigned BitWidth = Ext->getType()->getIntegerBitWidth();
2565 APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth)
2566 : Cst->getValue().zext(BitWidth);
2567 TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002568 continue;
2569 }
2570 // UndefValue are typed, so we have to statically sign extend them.
2571 if (isa<UndefValue>(Opnd)) {
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002572 DEBUG(dbgs() << "Statically extend\n");
2573 TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType()));
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002574 continue;
2575 }
2576
2577 // Otherwise we have to explicity sign extend the operand.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002578 // Check if Ext was reused to extend an operand.
2579 if (!ExtForOpnd) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002580 // If yes, create a new one.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002581 DEBUG(dbgs() << "More operands to ext\n");
Quentin Colombet84f89cc2014-12-22 18:11:52 +00002582 Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->getType())
2583 : TPT.createZExt(Ext, Opnd, Ext->getType());
2584 if (!isa<Instruction>(ValForExtOpnd)) {
2585 TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd);
2586 continue;
2587 }
2588 ExtForOpnd = cast<Instruction>(ValForExtOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002589 }
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002590 if (Exts)
2591 Exts->push_back(ExtForOpnd);
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002592 TPT.setOperand(ExtForOpnd, 0, Opnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002593
2594 // Move the sign extension before the insertion point.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002595 TPT.moveBefore(ExtForOpnd, ExtOpnd);
2596 TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd);
Quentin Colombet1b274f92015-03-10 21:48:15 +00002597 CreatedInstsCost += !TLI.isExtFree(ExtForOpnd);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002598 // If more sext are required, new instructions will have to be created.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002599 ExtForOpnd = nullptr;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002600 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002601 if (ExtForOpnd == Ext) {
2602 DEBUG(dbgs() << "Extension is useless now\n");
2603 TPT.eraseInstruction(Ext);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002604 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002605 return ExtOpnd;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002606}
2607
Quentin Colombet867c5502014-02-14 22:23:22 +00002608/// IsPromotionProfitable - Check whether or not promoting an instruction
2609/// to a wider type was profitable.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002610/// \p NewCost gives the cost of extension instructions created by the
2611/// promotion.
2612/// \p OldCost gives the cost of extension instructions before the promotion
2613/// plus the number of instructions that have been
2614/// matched in the addressing mode the promotion.
Quentin Colombet867c5502014-02-14 22:23:22 +00002615/// \p PromotedOperand is the value that has been promoted.
2616/// \return True if the promotion is profitable, false otherwise.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002617bool AddressingModeMatcher::IsPromotionProfitable(
2618 unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const {
2619 DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n');
2620 // The cost of the new extensions is greater than the cost of the
2621 // old extension plus what we folded.
Quentin Colombet867c5502014-02-14 22:23:22 +00002622 // This is not profitable.
Quentin Colombet1b274f92015-03-10 21:48:15 +00002623 if (NewCost > OldCost)
Quentin Colombet867c5502014-02-14 22:23:22 +00002624 return false;
Quentin Colombet1b274f92015-03-10 21:48:15 +00002625 if (NewCost < OldCost)
Quentin Colombet867c5502014-02-14 22:23:22 +00002626 return true;
2627 // The promotion is neutral but it may help folding the sign extension in
2628 // loads for instance.
2629 // Check that we did not create an illegal instruction.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002630 return isPromotedInstructionLegal(TLI, PromotedOperand);
Quentin Colombet867c5502014-02-14 22:23:22 +00002631}
2632
Chandler Carruthc8925912013-01-05 02:09:22 +00002633/// MatchOperationAddr - Given an instruction or constant expr, see if we can
2634/// fold the operation into the addressing mode. If so, update the addressing
2635/// mode and return true, otherwise return false without modifying AddrMode.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002636/// If \p MovedAway is not NULL, it contains the information of whether or
2637/// not AddrInst has to be folded into the addressing mode on success.
2638/// If \p MovedAway == true, \p AddrInst will not be part of the addressing
2639/// because it has been moved away.
2640/// Thus AddrInst must not be added in the matched instructions.
2641/// This state can happen when AddrInst is a sext, since it may be moved away.
2642/// Therefore, AddrInst may not be valid when MovedAway is true and it must
2643/// not be referenced anymore.
Chandler Carruthc8925912013-01-05 02:09:22 +00002644bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002645 unsigned Depth,
2646 bool *MovedAway) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002647 // Avoid exponential behavior on extremely deep expression trees.
2648 if (Depth >= 5) return false;
Stephen Lin837bba12013-07-15 17:55:02 +00002649
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002650 // By default, all matched instructions stay in place.
2651 if (MovedAway)
2652 *MovedAway = false;
2653
Chandler Carruthc8925912013-01-05 02:09:22 +00002654 switch (Opcode) {
2655 case Instruction::PtrToInt:
2656 // PtrToInt is always a noop, as we know that the int type is pointer sized.
2657 return MatchAddr(AddrInst->getOperand(0), Depth);
2658 case Instruction::IntToPtr:
2659 // This inttoptr is a no-op if the integer type is pointer sized.
2660 if (TLI.getValueType(AddrInst->getOperand(0)->getType()) ==
Matt Arsenault37d42ec2013-09-06 00:18:43 +00002661 TLI.getPointerTy(AddrInst->getType()->getPointerAddressSpace()))
Chandler Carruthc8925912013-01-05 02:09:22 +00002662 return MatchAddr(AddrInst->getOperand(0), Depth);
2663 return false;
2664 case Instruction::BitCast:
Eli Benderskyf13a0562014-05-22 00:02:52 +00002665 case Instruction::AddrSpaceCast:
Chandler Carruthc8925912013-01-05 02:09:22 +00002666 // BitCast is always a noop, and we can handle it as long as it is
2667 // int->int or pointer->pointer (we don't want int<->fp or something).
2668 if ((AddrInst->getOperand(0)->getType()->isPointerTy() ||
2669 AddrInst->getOperand(0)->getType()->isIntegerTy()) &&
2670 // Don't touch identity bitcasts. These were probably put here by LSR,
2671 // and we don't want to mess around with them. Assume it knows what it
2672 // is doing.
2673 AddrInst->getOperand(0)->getType() != AddrInst->getType())
2674 return MatchAddr(AddrInst->getOperand(0), Depth);
2675 return false;
2676 case Instruction::Add: {
2677 // Check to see if we can merge in the RHS then the LHS. If so, we win.
2678 ExtAddrMode BackupAddrMode = AddrMode;
2679 unsigned OldSize = AddrModeInsts.size();
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002680 // Start a transaction at this point.
2681 // The LHS may match but not the RHS.
2682 // Therefore, we need a higher level restoration point to undo partially
2683 // matched operation.
2684 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
2685 TPT.getRestorationPoint();
2686
Chandler Carruthc8925912013-01-05 02:09:22 +00002687 if (MatchAddr(AddrInst->getOperand(1), Depth+1) &&
2688 MatchAddr(AddrInst->getOperand(0), Depth+1))
2689 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00002690
Chandler Carruthc8925912013-01-05 02:09:22 +00002691 // Restore the old addr mode info.
2692 AddrMode = BackupAddrMode;
2693 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002694 TPT.rollback(LastKnownGood);
Stephen Lin837bba12013-07-15 17:55:02 +00002695
Chandler Carruthc8925912013-01-05 02:09:22 +00002696 // Otherwise this was over-aggressive. Try merging in the LHS then the RHS.
2697 if (MatchAddr(AddrInst->getOperand(0), Depth+1) &&
2698 MatchAddr(AddrInst->getOperand(1), Depth+1))
2699 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00002700
Chandler Carruthc8925912013-01-05 02:09:22 +00002701 // Otherwise we definitely can't merge the ADD in.
2702 AddrMode = BackupAddrMode;
2703 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002704 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00002705 break;
2706 }
2707 //case Instruction::Or:
2708 // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
2709 //break;
2710 case Instruction::Mul:
2711 case Instruction::Shl: {
2712 // Can only handle X*C and X << C.
2713 ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
Sanjay Pateld3bbfa12014-07-16 22:40:28 +00002714 if (!RHS)
2715 return false;
Chandler Carruthc8925912013-01-05 02:09:22 +00002716 int64_t Scale = RHS->getSExtValue();
2717 if (Opcode == Instruction::Shl)
2718 Scale = 1LL << Scale;
Stephen Lin837bba12013-07-15 17:55:02 +00002719
Chandler Carruthc8925912013-01-05 02:09:22 +00002720 return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth);
2721 }
2722 case Instruction::GetElementPtr: {
2723 // Scan the GEP. We check it if it contains constant offsets and at most
2724 // one variable offset.
2725 int VariableOperand = -1;
2726 unsigned VariableScale = 0;
Stephen Lin837bba12013-07-15 17:55:02 +00002727
Chandler Carruthc8925912013-01-05 02:09:22 +00002728 int64_t ConstantOffset = 0;
2729 const DataLayout *TD = TLI.getDataLayout();
2730 gep_type_iterator GTI = gep_type_begin(AddrInst);
2731 for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
2732 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
2733 const StructLayout *SL = TD->getStructLayout(STy);
2734 unsigned Idx =
2735 cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
2736 ConstantOffset += SL->getElementOffset(Idx);
2737 } else {
2738 uint64_t TypeSize = TD->getTypeAllocSize(GTI.getIndexedType());
2739 if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
2740 ConstantOffset += CI->getSExtValue()*TypeSize;
2741 } else if (TypeSize) { // Scales of zero don't do anything.
2742 // We only allow one variable index at the moment.
2743 if (VariableOperand != -1)
2744 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00002745
Chandler Carruthc8925912013-01-05 02:09:22 +00002746 // Remember the variable index.
2747 VariableOperand = i;
2748 VariableScale = TypeSize;
2749 }
2750 }
2751 }
Stephen Lin837bba12013-07-15 17:55:02 +00002752
Chandler Carruthc8925912013-01-05 02:09:22 +00002753 // A common case is for the GEP to only do a constant offset. In this case,
2754 // just add it to the disp field and check validity.
2755 if (VariableOperand == -1) {
2756 AddrMode.BaseOffs += ConstantOffset;
2757 if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){
2758 // Check to see if we can fold the base pointer in too.
2759 if (MatchAddr(AddrInst->getOperand(0), Depth+1))
2760 return true;
2761 }
2762 AddrMode.BaseOffs -= ConstantOffset;
2763 return false;
2764 }
2765
2766 // Save the valid addressing mode in case we can't match.
2767 ExtAddrMode BackupAddrMode = AddrMode;
2768 unsigned OldSize = AddrModeInsts.size();
2769
2770 // See if the scale and offset amount is valid for this target.
2771 AddrMode.BaseOffs += ConstantOffset;
2772
2773 // Match the base operand of the GEP.
2774 if (!MatchAddr(AddrInst->getOperand(0), Depth+1)) {
2775 // If it couldn't be matched, just stuff the value in a register.
2776 if (AddrMode.HasBaseReg) {
2777 AddrMode = BackupAddrMode;
2778 AddrModeInsts.resize(OldSize);
2779 return false;
2780 }
2781 AddrMode.HasBaseReg = true;
2782 AddrMode.BaseReg = AddrInst->getOperand(0);
2783 }
2784
2785 // Match the remaining variable portion of the GEP.
2786 if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
2787 Depth)) {
2788 // If it couldn't be matched, try stuffing the base into a register
2789 // instead of matching it, and retrying the match of the scale.
2790 AddrMode = BackupAddrMode;
2791 AddrModeInsts.resize(OldSize);
2792 if (AddrMode.HasBaseReg)
2793 return false;
2794 AddrMode.HasBaseReg = true;
2795 AddrMode.BaseReg = AddrInst->getOperand(0);
2796 AddrMode.BaseOffs += ConstantOffset;
2797 if (!MatchScaledValue(AddrInst->getOperand(VariableOperand),
2798 VariableScale, Depth)) {
2799 // If even that didn't work, bail.
2800 AddrMode = BackupAddrMode;
2801 AddrModeInsts.resize(OldSize);
2802 return false;
2803 }
2804 }
2805
2806 return true;
2807 }
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002808 case Instruction::SExt:
2809 case Instruction::ZExt: {
2810 Instruction *Ext = dyn_cast<Instruction>(AddrInst);
2811 if (!Ext)
Sanjay Pateld3bbfa12014-07-16 22:40:28 +00002812 return false;
Sanjay Patelab60d042014-07-16 21:08:10 +00002813
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002814 // Try to move this ext out of the way of the addressing mode.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002815 // Ask for a method for doing so.
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002816 TypePromotionHelper::Action TPH =
2817 TypePromotionHelper::getAction(Ext, InsertedTruncs, TLI, PromotedInsts);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002818 if (!TPH)
2819 return false;
2820
2821 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
2822 TPT.getRestorationPoint();
Quentin Colombet1b274f92015-03-10 21:48:15 +00002823 unsigned CreatedInstsCost = 0;
2824 unsigned ExtCost = !TLI.isExtFree(Ext);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00002825 Value *PromotedOperand =
Quentin Colombet1b274f92015-03-10 21:48:15 +00002826 TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002827 // SExt has been moved away.
2828 // Thus either it will be rematched later in the recursive calls or it is
2829 // gone. Anyway, we must not fold it into the addressing mode at this point.
2830 // E.g.,
2831 // op = add opnd, 1
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002832 // idx = ext op
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002833 // addr = gep base, idx
2834 // is now:
Quentin Colombetf5485bb2014-11-13 01:44:51 +00002835 // promotedOpnd = ext opnd <- no match here
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002836 // op = promoted_add promotedOpnd, 1 <- match (later in recursive calls)
2837 // addr = gep base, op <- match
2838 if (MovedAway)
2839 *MovedAway = true;
2840
2841 assert(PromotedOperand &&
2842 "TypePromotionHelper should have filtered out those cases");
2843
2844 ExtAddrMode BackupAddrMode = AddrMode;
2845 unsigned OldSize = AddrModeInsts.size();
2846
2847 if (!MatchAddr(PromotedOperand, Depth) ||
Quentin Colombet1b274f92015-03-10 21:48:15 +00002848 // The total of the new cost is equals to the cost of the created
2849 // instructions.
2850 // The total of the old cost is equals to the cost of the extension plus
2851 // what we have saved in the addressing mode.
2852 !IsPromotionProfitable(CreatedInstsCost,
2853 ExtCost + (AddrModeInsts.size() - OldSize),
Quentin Colombet867c5502014-02-14 22:23:22 +00002854 PromotedOperand)) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002855 AddrMode = BackupAddrMode;
2856 AddrModeInsts.resize(OldSize);
2857 DEBUG(dbgs() << "Sign extension does not pay off: rollback\n");
2858 TPT.rollback(LastKnownGood);
2859 return false;
2860 }
2861 return true;
2862 }
Chandler Carruthc8925912013-01-05 02:09:22 +00002863 }
2864 return false;
2865}
2866
2867/// MatchAddr - If we can, try to add the value of 'Addr' into the current
2868/// addressing mode. If Addr can't be added to AddrMode this returns false and
2869/// leaves AddrMode unmodified. This assumes that Addr is either a pointer type
2870/// or intptr_t for the target.
2871///
2872bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) {
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002873 // Start a transaction at this point that we will rollback if the matching
2874 // fails.
2875 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
2876 TPT.getRestorationPoint();
Chandler Carruthc8925912013-01-05 02:09:22 +00002877 if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
2878 // Fold in immediates if legal for the target.
2879 AddrMode.BaseOffs += CI->getSExtValue();
2880 if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
2881 return true;
2882 AddrMode.BaseOffs -= CI->getSExtValue();
2883 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
2884 // If this is a global variable, try to fold it into the addressing mode.
Craig Topperc0196b12014-04-14 00:51:57 +00002885 if (!AddrMode.BaseGV) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002886 AddrMode.BaseGV = GV;
2887 if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
2888 return true;
Craig Topperc0196b12014-04-14 00:51:57 +00002889 AddrMode.BaseGV = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00002890 }
2891 } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
2892 ExtAddrMode BackupAddrMode = AddrMode;
2893 unsigned OldSize = AddrModeInsts.size();
2894
2895 // Check to see if it is possible to fold this operation.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002896 bool MovedAway = false;
2897 if (MatchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) {
2898 // This instruction may have been move away. If so, there is nothing
2899 // to check here.
2900 if (MovedAway)
2901 return true;
Chandler Carruthc8925912013-01-05 02:09:22 +00002902 // Okay, it's possible to fold this. Check to see if it is actually
2903 // *profitable* to do so. We use a simple cost model to avoid increasing
2904 // register pressure too much.
2905 if (I->hasOneUse() ||
2906 IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
2907 AddrModeInsts.push_back(I);
2908 return true;
2909 }
Stephen Lin837bba12013-07-15 17:55:02 +00002910
Chandler Carruthc8925912013-01-05 02:09:22 +00002911 // It isn't profitable to do this, roll back.
2912 //cerr << "NOT FOLDING: " << *I;
2913 AddrMode = BackupAddrMode;
2914 AddrModeInsts.resize(OldSize);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002915 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00002916 }
2917 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
2918 if (MatchOperationAddr(CE, CE->getOpcode(), Depth))
2919 return true;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002920 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00002921 } else if (isa<ConstantPointerNull>(Addr)) {
2922 // Null pointer gets folded without affecting the addressing mode.
2923 return true;
2924 }
2925
2926 // Worse case, the target should support [reg] addressing modes. :)
2927 if (!AddrMode.HasBaseReg) {
2928 AddrMode.HasBaseReg = true;
2929 AddrMode.BaseReg = Addr;
2930 // Still check for legality in case the target supports [imm] but not [i+r].
2931 if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
2932 return true;
2933 AddrMode.HasBaseReg = false;
Craig Topperc0196b12014-04-14 00:51:57 +00002934 AddrMode.BaseReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00002935 }
2936
2937 // If the base register is already taken, see if we can do [r+r].
2938 if (AddrMode.Scale == 0) {
2939 AddrMode.Scale = 1;
2940 AddrMode.ScaledReg = Addr;
2941 if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
2942 return true;
2943 AddrMode.Scale = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002944 AddrMode.ScaledReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00002945 }
2946 // Couldn't match.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00002947 TPT.rollback(LastKnownGood);
Chandler Carruthc8925912013-01-05 02:09:22 +00002948 return false;
2949}
2950
2951/// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified
2952/// inline asm call are due to memory operands. If so, return true, otherwise
2953/// return false.
2954static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
Eric Christopher11e4df72015-02-26 22:38:43 +00002955 const TargetMachine &TM) {
2956 const Function *F = CI->getParent()->getParent();
2957 const TargetLowering *TLI = TM.getSubtargetImpl(*F)->getTargetLowering();
2958 const TargetRegisterInfo *TRI = TM.getSubtargetImpl(*F)->getRegisterInfo();
Eric Christopherd75c00c2015-02-26 22:38:34 +00002959 TargetLowering::AsmOperandInfoVector TargetConstraints =
Eric Christopher11e4df72015-02-26 22:38:43 +00002960 TLI->ParseConstraints(TRI, ImmutableCallSite(CI));
Chandler Carruthc8925912013-01-05 02:09:22 +00002961 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
2962 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
Stephen Lin837bba12013-07-15 17:55:02 +00002963
Chandler Carruthc8925912013-01-05 02:09:22 +00002964 // Compute the constraint code and ConstraintType to use.
Eric Christopher11e4df72015-02-26 22:38:43 +00002965 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chandler Carruthc8925912013-01-05 02:09:22 +00002966
2967 // If this asm operand is our Value*, and if it isn't an indirect memory
2968 // operand, we can't fold it!
2969 if (OpInfo.CallOperandVal == OpVal &&
2970 (OpInfo.ConstraintType != TargetLowering::C_Memory ||
2971 !OpInfo.isIndirect))
2972 return false;
2973 }
2974
2975 return true;
2976}
2977
2978/// FindAllMemoryUses - Recursively walk all the uses of I until we find a
2979/// memory use. If we find an obviously non-foldable instruction, return true.
2980/// Add the ultimately found memory instructions to MemoryUses.
Eric Christopher11e4df72015-02-26 22:38:43 +00002981static bool FindAllMemoryUses(
2982 Instruction *I,
2983 SmallVectorImpl<std::pair<Instruction *, unsigned>> &MemoryUses,
2984 SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetMachine &TM) {
Chandler Carruthc8925912013-01-05 02:09:22 +00002985 // If we already considered this instruction, we're done.
David Blaikie70573dc2014-11-19 07:49:26 +00002986 if (!ConsideredInsts.insert(I).second)
Chandler Carruthc8925912013-01-05 02:09:22 +00002987 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00002988
Chandler Carruthc8925912013-01-05 02:09:22 +00002989 // If this is an obviously unfoldable instruction, bail out.
2990 if (!MightBeFoldableInst(I))
2991 return true;
2992
2993 // Loop over all the uses, recursively processing them.
Chandler Carruthcdf47882014-03-09 03:16:01 +00002994 for (Use &U : I->uses()) {
2995 Instruction *UserI = cast<Instruction>(U.getUser());
Chandler Carruthc8925912013-01-05 02:09:22 +00002996
Chandler Carruthcdf47882014-03-09 03:16:01 +00002997 if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) {
2998 MemoryUses.push_back(std::make_pair(LI, U.getOperandNo()));
Chandler Carruthc8925912013-01-05 02:09:22 +00002999 continue;
3000 }
Stephen Lin837bba12013-07-15 17:55:02 +00003001
Chandler Carruthcdf47882014-03-09 03:16:01 +00003002 if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
3003 unsigned opNo = U.getOperandNo();
Chandler Carruthc8925912013-01-05 02:09:22 +00003004 if (opNo == 0) return true; // Storing addr, not into addr.
3005 MemoryUses.push_back(std::make_pair(SI, opNo));
3006 continue;
3007 }
Stephen Lin837bba12013-07-15 17:55:02 +00003008
Chandler Carruthcdf47882014-03-09 03:16:01 +00003009 if (CallInst *CI = dyn_cast<CallInst>(UserI)) {
Chandler Carruthc8925912013-01-05 02:09:22 +00003010 InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue());
3011 if (!IA) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003012
Chandler Carruthc8925912013-01-05 02:09:22 +00003013 // If this is a memory operand, we're cool, otherwise bail out.
Eric Christopher11e4df72015-02-26 22:38:43 +00003014 if (!IsOperandAMemoryOperand(CI, IA, I, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003015 return true;
3016 continue;
3017 }
Stephen Lin837bba12013-07-15 17:55:02 +00003018
Eric Christopher11e4df72015-02-26 22:38:43 +00003019 if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003020 return true;
3021 }
3022
3023 return false;
3024}
3025
3026/// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at
3027/// the use site that we're folding it into. If so, there is no cost to
3028/// include it in the addressing mode. KnownLive1 and KnownLive2 are two values
3029/// that we know are live at the instruction already.
3030bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1,
3031 Value *KnownLive2) {
3032 // If Val is either of the known-live values, we know it is live!
Craig Topperc0196b12014-04-14 00:51:57 +00003033 if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2)
Chandler Carruthc8925912013-01-05 02:09:22 +00003034 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003035
Chandler Carruthc8925912013-01-05 02:09:22 +00003036 // All values other than instructions and arguments (e.g. constants) are live.
3037 if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003038
Chandler Carruthc8925912013-01-05 02:09:22 +00003039 // If Val is a constant sized alloca in the entry block, it is live, this is
3040 // true because it is just a reference to the stack/frame pointer, which is
3041 // live for the whole function.
3042 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
3043 if (AI->isStaticAlloca())
3044 return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003045
Chandler Carruthc8925912013-01-05 02:09:22 +00003046 // Check to see if this value is already used in the memory instruction's
3047 // block. If so, it's already live into the block at the very least, so we
3048 // can reasonably fold it.
3049 return Val->isUsedInBasicBlock(MemoryInst->getParent());
3050}
3051
3052/// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing
3053/// mode of the machine to fold the specified instruction into a load or store
3054/// that ultimately uses it. However, the specified instruction has multiple
3055/// uses. Given this, it may actually increase register pressure to fold it
3056/// into the load. For example, consider this code:
3057///
3058/// X = ...
3059/// Y = X+1
3060/// use(Y) -> nonload/store
3061/// Z = Y+1
3062/// load Z
3063///
3064/// In this case, Y has multiple uses, and can be folded into the load of Z
3065/// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to
3066/// be live at the use(Y) line. If we don't fold Y into load Z, we use one
3067/// fewer register. Since Y can't be folded into "use(Y)" we don't increase the
3068/// number of computations either.
3069///
3070/// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If
3071/// X was live across 'load Z' for other reasons, we actually *would* want to
3072/// fold the addressing mode in the Z case. This would make Y die earlier.
3073bool AddressingModeMatcher::
3074IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore,
3075 ExtAddrMode &AMAfter) {
3076 if (IgnoreProfitability) return true;
Stephen Lin837bba12013-07-15 17:55:02 +00003077
Chandler Carruthc8925912013-01-05 02:09:22 +00003078 // AMBefore is the addressing mode before this instruction was folded into it,
3079 // and AMAfter is the addressing mode after the instruction was folded. Get
3080 // the set of registers referenced by AMAfter and subtract out those
3081 // referenced by AMBefore: this is the set of values which folding in this
3082 // address extends the lifetime of.
3083 //
3084 // Note that there are only two potential values being referenced here,
3085 // BaseReg and ScaleReg (global addresses are always available, as are any
3086 // folded immediates).
3087 Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
Stephen Lin837bba12013-07-15 17:55:02 +00003088
Chandler Carruthc8925912013-01-05 02:09:22 +00003089 // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
3090 // lifetime wasn't extended by adding this instruction.
3091 if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
Craig Topperc0196b12014-04-14 00:51:57 +00003092 BaseReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003093 if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
Craig Topperc0196b12014-04-14 00:51:57 +00003094 ScaledReg = nullptr;
Chandler Carruthc8925912013-01-05 02:09:22 +00003095
3096 // If folding this instruction (and it's subexprs) didn't extend any live
3097 // ranges, we're ok with it.
Craig Topperc0196b12014-04-14 00:51:57 +00003098 if (!BaseReg && !ScaledReg)
Chandler Carruthc8925912013-01-05 02:09:22 +00003099 return true;
3100
3101 // If all uses of this instruction are ultimately load/store/inlineasm's,
3102 // check to see if their addressing modes will include this instruction. If
3103 // so, we can fold it into all uses, so it doesn't matter if it has multiple
3104 // uses.
3105 SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses;
3106 SmallPtrSet<Instruction*, 16> ConsideredInsts;
Eric Christopher11e4df72015-02-26 22:38:43 +00003107 if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TM))
Chandler Carruthc8925912013-01-05 02:09:22 +00003108 return false; // Has a non-memory, non-foldable use!
Stephen Lin837bba12013-07-15 17:55:02 +00003109
Chandler Carruthc8925912013-01-05 02:09:22 +00003110 // Now that we know that all uses of this instruction are part of a chain of
3111 // computation involving only operations that could theoretically be folded
3112 // into a memory use, loop over each of these uses and see if they could
3113 // *actually* fold the instruction.
3114 SmallVector<Instruction*, 32> MatchedAddrModeInsts;
3115 for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) {
3116 Instruction *User = MemoryUses[i].first;
3117 unsigned OpNo = MemoryUses[i].second;
Stephen Lin837bba12013-07-15 17:55:02 +00003118
Chandler Carruthc8925912013-01-05 02:09:22 +00003119 // Get the access type of this use. If the use isn't a pointer, we don't
3120 // know what it accesses.
3121 Value *Address = User->getOperand(OpNo);
3122 if (!Address->getType()->isPointerTy())
3123 return false;
Matt Arsenault8227b9f2013-09-06 00:37:24 +00003124 Type *AddressAccessTy = Address->getType()->getPointerElementType();
Stephen Lin837bba12013-07-15 17:55:02 +00003125
Chandler Carruthc8925912013-01-05 02:09:22 +00003126 // Do a match against the root of this address, ignoring profitability. This
3127 // will tell us if the addressing mode for the memory operation will
3128 // *actually* cover the shared instruction.
3129 ExtAddrMode Result;
Quentin Colombet5a69dda2014-02-11 01:59:02 +00003130 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3131 TPT.getRestorationPoint();
Eric Christopherd75c00c2015-02-26 22:38:34 +00003132 AddressingModeMatcher Matcher(MatchedAddrModeInsts, TM, AddressAccessTy,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003133 MemoryInst, Result, InsertedTruncs,
3134 PromotedInsts, TPT);
Chandler Carruthc8925912013-01-05 02:09:22 +00003135 Matcher.IgnoreProfitability = true;
3136 bool Success = Matcher.MatchAddr(Address, 0);
3137 (void)Success; assert(Success && "Couldn't select *anything*?");
3138
Quentin Colombet5a69dda2014-02-11 01:59:02 +00003139 // The match was to check the profitability, the changes made are not
3140 // part of the original matcher. Therefore, they should be dropped
3141 // otherwise the original matcher will not present the right state.
3142 TPT.rollback(LastKnownGood);
3143
Chandler Carruthc8925912013-01-05 02:09:22 +00003144 // If the match didn't cover I, then it won't be shared by it.
3145 if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(),
3146 I) == MatchedAddrModeInsts.end())
3147 return false;
Stephen Lin837bba12013-07-15 17:55:02 +00003148
Chandler Carruthc8925912013-01-05 02:09:22 +00003149 MatchedAddrModeInsts.clear();
3150 }
Stephen Lin837bba12013-07-15 17:55:02 +00003151
Chandler Carruthc8925912013-01-05 02:09:22 +00003152 return true;
3153}
3154
3155} // end anonymous namespace
3156
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003157/// IsNonLocalValue - Return true if the specified values are defined in a
3158/// different basic block than BB.
3159static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
3160 if (Instruction *I = dyn_cast<Instruction>(V))
3161 return I->getParent() != BB;
3162 return false;
3163}
3164
Bob Wilson53bdae32009-12-03 21:47:07 +00003165/// OptimizeMemoryInst - Load and Store Instructions often have
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003166/// addressing modes that can do significant amounts of computation. As such,
3167/// instruction selection will try to get the load or store to do as much
3168/// computation as possible for the program. The problem is that isel can only
3169/// see within a single block. As such, we sink as much legal addressing mode
3170/// stuff into the block as possible.
Chris Lattner728f9022008-11-25 07:09:13 +00003171///
3172/// This method is used to optimize both load/store and inline asms with memory
3173/// operands.
Chris Lattner6d71b7f2008-11-26 03:20:37 +00003174bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
Chris Lattner229907c2011-07-18 04:54:35 +00003175 Type *AccessTy) {
Owen Anderson8ba5f392010-11-27 08:15:55 +00003176 Value *Repl = Addr;
Nadav Rotem465834c2012-07-24 10:51:42 +00003177
3178 // Try to collapse single-value PHI nodes. This is necessary to undo
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003179 // unprofitable PRE transformations.
Cameron Zwarich43cecb12011-01-03 06:33:01 +00003180 SmallVector<Value*, 8> worklist;
3181 SmallPtrSet<Value*, 16> Visited;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003182 worklist.push_back(Addr);
Nadav Rotem465834c2012-07-24 10:51:42 +00003183
Owen Anderson8ba5f392010-11-27 08:15:55 +00003184 // Use a worklist to iteratively look through PHI nodes, and ensure that
3185 // the addressing mode obtained from the non-PHI roots of the graph
3186 // are equivalent.
Craig Topperc0196b12014-04-14 00:51:57 +00003187 Value *Consensus = nullptr;
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003188 unsigned NumUsesConsensus = 0;
Cameron Zwarich13c885d2011-03-05 08:12:26 +00003189 bool IsNumUsesConsensusValid = false;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003190 SmallVector<Instruction*, 16> AddrModeInsts;
3191 ExtAddrMode AddrMode;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003192 TypePromotionTransaction TPT;
3193 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3194 TPT.getRestorationPoint();
Owen Anderson8ba5f392010-11-27 08:15:55 +00003195 while (!worklist.empty()) {
3196 Value *V = worklist.back();
3197 worklist.pop_back();
Nadav Rotem465834c2012-07-24 10:51:42 +00003198
Owen Anderson8ba5f392010-11-27 08:15:55 +00003199 // Break use-def graph loops.
David Blaikie70573dc2014-11-19 07:49:26 +00003200 if (!Visited.insert(V).second) {
Craig Topperc0196b12014-04-14 00:51:57 +00003201 Consensus = nullptr;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003202 break;
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003203 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003204
Owen Anderson8ba5f392010-11-27 08:15:55 +00003205 // For a PHI node, push all of its incoming values.
3206 if (PHINode *P = dyn_cast<PHINode>(V)) {
3207 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i)
3208 worklist.push_back(P->getIncomingValue(i));
3209 continue;
3210 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003211
Owen Anderson8ba5f392010-11-27 08:15:55 +00003212 // For non-PHIs, determine the addressing mode being computed.
3213 SmallVector<Instruction*, 16> NewAddrModeInsts;
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003214 ExtAddrMode NewAddrMode = AddressingModeMatcher::Match(
Eric Christopherd75c00c2015-02-26 22:38:34 +00003215 V, AccessTy, MemoryInst, NewAddrModeInsts, *TM, InsertedTruncsSet,
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003216 PromotedInsts, TPT);
Cameron Zwarich13c885d2011-03-05 08:12:26 +00003217
3218 // This check is broken into two cases with very similar code to avoid using
3219 // getNumUses() as much as possible. Some values have a lot of uses, so
3220 // calling getNumUses() unconditionally caused a significant compile-time
3221 // regression.
3222 if (!Consensus) {
3223 Consensus = V;
3224 AddrMode = NewAddrMode;
3225 AddrModeInsts = NewAddrModeInsts;
3226 continue;
3227 } else if (NewAddrMode == AddrMode) {
3228 if (!IsNumUsesConsensusValid) {
3229 NumUsesConsensus = Consensus->getNumUses();
3230 IsNumUsesConsensusValid = true;
3231 }
3232
3233 // Ensure that the obtained addressing mode is equivalent to that obtained
3234 // for all other roots of the PHI traversal. Also, when choosing one
3235 // such root as representative, select the one with the most uses in order
3236 // to keep the cost modeling heuristics in AddressingModeMatcher
3237 // applicable.
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003238 unsigned NumUses = V->getNumUses();
3239 if (NumUses > NumUsesConsensus) {
Owen Anderson8ba5f392010-11-27 08:15:55 +00003240 Consensus = V;
Cameron Zwarichb7f8eaa2011-03-01 21:13:53 +00003241 NumUsesConsensus = NumUses;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003242 AddrModeInsts = NewAddrModeInsts;
3243 }
3244 continue;
3245 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003246
Craig Topperc0196b12014-04-14 00:51:57 +00003247 Consensus = nullptr;
Owen Anderson8ba5f392010-11-27 08:15:55 +00003248 break;
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003249 }
Nadav Rotem465834c2012-07-24 10:51:42 +00003250
Owen Anderson8ba5f392010-11-27 08:15:55 +00003251 // If the addressing mode couldn't be determined, or if multiple different
3252 // ones were determined, bail out now.
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003253 if (!Consensus) {
3254 TPT.rollback(LastKnownGood);
3255 return false;
3256 }
3257 TPT.commit();
Nadav Rotem465834c2012-07-24 10:51:42 +00003258
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003259 // Check to see if any of the instructions supersumed by this addr mode are
3260 // non-local to I's BB.
3261 bool AnyNonLocal = false;
3262 for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
Chris Lattner6d71b7f2008-11-26 03:20:37 +00003263 if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003264 AnyNonLocal = true;
3265 break;
3266 }
3267 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003268
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003269 // If all the instructions matched are already in this BB, don't do anything.
3270 if (!AnyNonLocal) {
David Greene74e2d492010-01-05 01:27:11 +00003271 DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003272 return false;
3273 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003274
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003275 // Insert this computation right after this user. Since our caller is
3276 // scanning from the top of the BB to the bottom, reuse of the expr are
3277 // guaranteed to happen later.
Devang Patelc10e52a2011-09-06 18:49:53 +00003278 IRBuilder<> Builder(MemoryInst);
Eric Christopherc1ea1492008-09-24 05:32:41 +00003279
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003280 // Now that we determined the addressing expression we want to use and know
3281 // that we have to sink it into this block. Check to see if we have already
3282 // done this for some other load/store instr in this block. If so, reuse the
3283 // computation.
3284 Value *&SunkAddr = SunkAddrs[Addr];
3285 if (SunkAddr) {
David Greene74e2d492010-01-05 01:27:11 +00003286 DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003287 << *MemoryInst << "\n");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003288 if (SunkAddr->getType() != Addr->getType())
Benjamin Kramer547b6c52011-09-27 20:39:19 +00003289 SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType());
Eric Christopherfccff372015-01-27 01:01:38 +00003290 } else if (AddrSinkUsingGEPs ||
3291 (!AddrSinkUsingGEPs.getNumOccurrences() && TM &&
Eric Christopher2c635492015-01-27 07:54:39 +00003292 TM->getSubtargetImpl(*MemoryInst->getParent()->getParent())
3293 ->useAA())) {
Hal Finkelc3998302014-04-12 00:59:48 +00003294 // By default, we use the GEP-based method when AA is used later. This
3295 // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities.
3296 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003297 << *MemoryInst << "\n");
Hal Finkelc3998302014-04-12 00:59:48 +00003298 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType());
Craig Topperc0196b12014-04-14 00:51:57 +00003299 Value *ResultPtr = nullptr, *ResultIndex = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003300
3301 // First, find the pointer.
3302 if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) {
3303 ResultPtr = AddrMode.BaseReg;
Craig Topperc0196b12014-04-14 00:51:57 +00003304 AddrMode.BaseReg = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003305 }
3306
3307 if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) {
3308 // We can't add more than one pointer together, nor can we scale a
3309 // pointer (both of which seem meaningless).
3310 if (ResultPtr || AddrMode.Scale != 1)
3311 return false;
3312
3313 ResultPtr = AddrMode.ScaledReg;
3314 AddrMode.Scale = 0;
3315 }
3316
3317 if (AddrMode.BaseGV) {
3318 if (ResultPtr)
3319 return false;
3320
3321 ResultPtr = AddrMode.BaseGV;
3322 }
3323
3324 // If the real base value actually came from an inttoptr, then the matcher
3325 // will look through it and provide only the integer value. In that case,
3326 // use it here.
3327 if (!ResultPtr && AddrMode.BaseReg) {
3328 ResultPtr =
3329 Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), "sunkaddr");
Craig Topperc0196b12014-04-14 00:51:57 +00003330 AddrMode.BaseReg = nullptr;
Hal Finkelc3998302014-04-12 00:59:48 +00003331 } else if (!ResultPtr && AddrMode.Scale == 1) {
3332 ResultPtr =
3333 Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), "sunkaddr");
3334 AddrMode.Scale = 0;
3335 }
3336
3337 if (!ResultPtr &&
3338 !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) {
3339 SunkAddr = Constant::getNullValue(Addr->getType());
3340 } else if (!ResultPtr) {
3341 return false;
3342 } else {
3343 Type *I8PtrTy =
David Blaikie3909da72015-03-30 20:42:56 +00003344 Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace());
3345 Type *I8Ty = Builder.getInt8Ty();
Hal Finkelc3998302014-04-12 00:59:48 +00003346
3347 // Start with the base register. Do this first so that subsequent address
3348 // matching finds it last, which will prevent it from trying to match it
3349 // as the scaled value in case it happens to be a mul. That would be
3350 // problematic if we've sunk a different mul for the scale, because then
3351 // we'd end up sinking both muls.
3352 if (AddrMode.BaseReg) {
3353 Value *V = AddrMode.BaseReg;
3354 if (V->getType() != IntPtrTy)
3355 V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
3356
3357 ResultIndex = V;
3358 }
3359
3360 // Add the scale value.
3361 if (AddrMode.Scale) {
3362 Value *V = AddrMode.ScaledReg;
3363 if (V->getType() == IntPtrTy) {
3364 // done.
3365 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
3366 cast<IntegerType>(V->getType())->getBitWidth()) {
3367 V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
3368 } else {
3369 // It is only safe to sign extend the BaseReg if we know that the math
3370 // required to create it did not overflow before we extend it. Since
3371 // the original IR value was tossed in favor of a constant back when
3372 // the AddrMode was created we need to bail out gracefully if widths
3373 // do not match instead of extending it.
3374 Instruction *I = dyn_cast_or_null<Instruction>(ResultIndex);
3375 if (I && (ResultIndex != AddrMode.BaseReg))
3376 I->eraseFromParent();
3377 return false;
3378 }
3379
3380 if (AddrMode.Scale != 1)
3381 V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
3382 "sunkaddr");
3383 if (ResultIndex)
3384 ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr");
3385 else
3386 ResultIndex = V;
3387 }
3388
3389 // Add in the Base Offset if present.
3390 if (AddrMode.BaseOffs) {
3391 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
3392 if (ResultIndex) {
NAKAMURA Takumif51a34e2014-10-29 15:23:11 +00003393 // We need to add this separately from the scale above to help with
3394 // SDAG consecutive load/store merging.
Hal Finkelc3998302014-04-12 00:59:48 +00003395 if (ResultPtr->getType() != I8PtrTy)
3396 ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
David Blaikie3909da72015-03-30 20:42:56 +00003397 ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
Hal Finkelc3998302014-04-12 00:59:48 +00003398 }
3399
3400 ResultIndex = V;
3401 }
3402
3403 if (!ResultIndex) {
3404 SunkAddr = ResultPtr;
3405 } else {
3406 if (ResultPtr->getType() != I8PtrTy)
3407 ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
David Blaikie3909da72015-03-30 20:42:56 +00003408 SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
Hal Finkelc3998302014-04-12 00:59:48 +00003409 }
3410
3411 if (SunkAddr->getType() != Addr->getType())
3412 SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType());
3413 }
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003414 } else {
David Greene74e2d492010-01-05 01:27:11 +00003415 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
Louis Gerbarg1b91aa22014-05-13 21:54:22 +00003416 << *MemoryInst << "\n");
Matt Arsenault37d42ec2013-09-06 00:18:43 +00003417 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType());
Craig Topperc0196b12014-04-14 00:51:57 +00003418 Value *Result = nullptr;
Dan Gohmanca194452010-01-19 22:45:06 +00003419
3420 // Start with the base register. Do this first so that subsequent address
3421 // matching finds it last, which will prevent it from trying to match it
3422 // as the scaled value in case it happens to be a mul. That would be
3423 // problematic if we've sunk a different mul for the scale, because then
3424 // we'd end up sinking both muls.
3425 if (AddrMode.BaseReg) {
3426 Value *V = AddrMode.BaseReg;
Duncan Sands19d0b472010-02-16 11:11:14 +00003427 if (V->getType()->isPointerTy())
Devang Patelc10e52a2011-09-06 18:49:53 +00003428 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
Dan Gohmanca194452010-01-19 22:45:06 +00003429 if (V->getType() != IntPtrTy)
Devang Patelc10e52a2011-09-06 18:49:53 +00003430 V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
Dan Gohmanca194452010-01-19 22:45:06 +00003431 Result = V;
3432 }
3433
3434 // Add the scale value.
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003435 if (AddrMode.Scale) {
3436 Value *V = AddrMode.ScaledReg;
3437 if (V->getType() == IntPtrTy) {
3438 // done.
Duncan Sands19d0b472010-02-16 11:11:14 +00003439 } else if (V->getType()->isPointerTy()) {
Devang Patelc10e52a2011-09-06 18:49:53 +00003440 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003441 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
3442 cast<IntegerType>(V->getType())->getBitWidth()) {
Devang Patelc10e52a2011-09-06 18:49:53 +00003443 V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003444 } else {
Jim Grosbached2cd392014-03-26 17:27:01 +00003445 // It is only safe to sign extend the BaseReg if we know that the math
3446 // required to create it did not overflow before we extend it. Since
3447 // the original IR value was tossed in favor of a constant back when
3448 // the AddrMode was created we need to bail out gracefully if widths
3449 // do not match instead of extending it.
Joey Gouly12a8bf02014-05-13 15:42:45 +00003450 Instruction *I = dyn_cast_or_null<Instruction>(Result);
Jim Grosbach83b44e12014-04-10 00:27:45 +00003451 if (I && (Result != AddrMode.BaseReg))
3452 I->eraseFromParent();
Jim Grosbached2cd392014-03-26 17:27:01 +00003453 return false;
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003454 }
3455 if (AddrMode.Scale != 1)
Devang Patelc10e52a2011-09-06 18:49:53 +00003456 V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
3457 "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003458 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00003459 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003460 else
3461 Result = V;
3462 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003463
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003464 // Add in the BaseGV if present.
3465 if (AddrMode.BaseGV) {
Devang Patelc10e52a2011-09-06 18:49:53 +00003466 Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003467 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00003468 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003469 else
3470 Result = V;
3471 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003472
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003473 // Add in the Base Offset if present.
3474 if (AddrMode.BaseOffs) {
Owen Andersonedb4a702009-07-24 23:12:02 +00003475 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003476 if (Result)
Devang Patelc10e52a2011-09-06 18:49:53 +00003477 Result = Builder.CreateAdd(Result, V, "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003478 else
3479 Result = V;
3480 }
3481
Craig Topperc0196b12014-04-14 00:51:57 +00003482 if (!Result)
Owen Anderson5a1acd92009-07-31 20:28:14 +00003483 SunkAddr = Constant::getNullValue(Addr->getType());
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003484 else
Devang Patelc10e52a2011-09-06 18:49:53 +00003485 SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003486 }
Eric Christopherc1ea1492008-09-24 05:32:41 +00003487
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003488 MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
Eric Christopherc1ea1492008-09-24 05:32:41 +00003489
Chris Lattneraf1bcce2011-04-09 07:05:44 +00003490 // If we have no uses, recursively delete the value and all dead instructions
3491 // using it.
Owen Andersondfb8c3b2010-11-19 22:15:03 +00003492 if (Repl->use_empty()) {
Chris Lattneraf1bcce2011-04-09 07:05:44 +00003493 // This can cause recursive deletion, which can invalidate our iterator.
3494 // Use a WeakVH to hold onto it in case this happens.
3495 WeakVH IterHandle(CurInstIterator);
3496 BasicBlock *BB = CurInstIterator->getParent();
Nadav Rotem465834c2012-07-24 10:51:42 +00003497
Benjamin Kramer8bcc9712012-08-29 15:32:21 +00003498 RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo);
Chris Lattneraf1bcce2011-04-09 07:05:44 +00003499
3500 if (IterHandle != CurInstIterator) {
3501 // If the iterator instruction was recursively deleted, start over at the
3502 // start of the block.
3503 CurInstIterator = BB->begin();
3504 SunkAddrs.clear();
Nadav Rotem465834c2012-07-24 10:51:42 +00003505 }
Dale Johannesenb67a6e662010-03-31 20:37:15 +00003506 }
Cameron Zwarichced753f2011-01-05 17:27:27 +00003507 ++NumMemoryInsts;
Chris Lattnerfeee64e2007-04-13 20:30:56 +00003508 return true;
3509}
3510
Evan Cheng1da25002008-02-26 02:42:37 +00003511/// OptimizeInlineAsmInst - If there are any memory operands, use
Chris Lattner728f9022008-11-25 07:09:13 +00003512/// OptimizeMemoryInst to sink their address computing into the block when
Evan Cheng1da25002008-02-26 02:42:37 +00003513/// possible / profitable.
Chris Lattner7a277142011-01-15 07:14:54 +00003514bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) {
Evan Cheng1da25002008-02-26 02:42:37 +00003515 bool MadeChange = false;
Evan Cheng1da25002008-02-26 02:42:37 +00003516
Eric Christopher11e4df72015-02-26 22:38:43 +00003517 const TargetRegisterInfo *TRI =
3518 TM->getSubtargetImpl(*CS->getParent()->getParent())->getRegisterInfo();
Nadav Rotem465834c2012-07-24 10:51:42 +00003519 TargetLowering::AsmOperandInfoVector
Eric Christopher11e4df72015-02-26 22:38:43 +00003520 TargetConstraints = TLI->ParseConstraints(TRI, CS);
Dale Johannesenf95f59a2010-09-16 18:30:55 +00003521 unsigned ArgNo = 0;
John Thompson1094c802010-09-13 18:15:37 +00003522 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
3523 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
Nadav Rotem465834c2012-07-24 10:51:42 +00003524
Evan Cheng1da25002008-02-26 02:42:37 +00003525 // Compute the constraint code and ConstraintType to use.
Dale Johannesence97d552010-06-25 21:55:36 +00003526 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Evan Cheng1da25002008-02-26 02:42:37 +00003527
Eli Friedman666bbe32008-02-26 18:37:49 +00003528 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
3529 OpInfo.isIndirect) {
Chris Lattner7a277142011-01-15 07:14:54 +00003530 Value *OpVal = CS->getArgOperand(ArgNo++);
Chris Lattneree588de2011-01-15 07:29:01 +00003531 MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType());
Dale Johannesenf95f59a2010-09-16 18:30:55 +00003532 } else if (OpInfo.Type == InlineAsm::isInput)
3533 ArgNo++;
Evan Cheng1da25002008-02-26 02:42:37 +00003534 }
3535
3536 return MadeChange;
3537}
3538
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003539/// \brief Check if all the uses of \p Inst are equivalent (or free) zero or
3540/// sign extensions.
3541static bool hasSameExtUse(Instruction *Inst, const TargetLowering &TLI) {
3542 assert(!Inst->use_empty() && "Input must have at least one use");
3543 const Instruction *FirstUser = cast<Instruction>(*Inst->user_begin());
3544 bool IsSExt = isa<SExtInst>(FirstUser);
3545 Type *ExtTy = FirstUser->getType();
3546 for (const User *U : Inst->users()) {
3547 const Instruction *UI = cast<Instruction>(U);
3548 if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI)))
3549 return false;
3550 Type *CurTy = UI->getType();
3551 // Same input and output types: Same instruction after CSE.
3552 if (CurTy == ExtTy)
3553 continue;
3554
3555 // If IsSExt is true, we are in this situation:
3556 // a = Inst
3557 // b = sext ty1 a to ty2
3558 // c = sext ty1 a to ty3
3559 // Assuming ty2 is shorter than ty3, this could be turned into:
3560 // a = Inst
3561 // b = sext ty1 a to ty2
3562 // c = sext ty2 b to ty3
3563 // However, the last sext is not free.
3564 if (IsSExt)
3565 return false;
3566
3567 // This is a ZExt, maybe this is free to extend from one type to another.
3568 // In that case, we would not account for a different use.
3569 Type *NarrowTy;
3570 Type *LargeTy;
3571 if (ExtTy->getScalarType()->getIntegerBitWidth() >
3572 CurTy->getScalarType()->getIntegerBitWidth()) {
3573 NarrowTy = CurTy;
3574 LargeTy = ExtTy;
3575 } else {
3576 NarrowTy = ExtTy;
3577 LargeTy = CurTy;
3578 }
3579
3580 if (!TLI.isZExtFree(NarrowTy, LargeTy))
3581 return false;
3582 }
3583 // All uses are the same or can be derived from one another for free.
3584 return true;
3585}
3586
3587/// \brief Try to form ExtLd by promoting \p Exts until they reach a
3588/// load instruction.
3589/// If an ext(load) can be formed, it is returned via \p LI for the load
3590/// and \p Inst for the extension.
3591/// Otherwise LI == nullptr and Inst == nullptr.
3592/// When some promotion happened, \p TPT contains the proper state to
3593/// revert them.
3594///
3595/// \return true when promoting was necessary to expose the ext(load)
3596/// opportunity, false otherwise.
3597///
3598/// Example:
3599/// \code
3600/// %ld = load i32* %addr
3601/// %add = add nuw i32 %ld, 4
3602/// %zext = zext i32 %add to i64
3603/// \endcode
3604/// =>
3605/// \code
3606/// %ld = load i32* %addr
3607/// %zext = zext i32 %ld to i64
3608/// %add = add nuw i64 %zext, 4
3609/// \encode
3610/// Thanks to the promotion, we can match zext(load i32*) to i64.
3611bool CodeGenPrepare::ExtLdPromotion(TypePromotionTransaction &TPT,
3612 LoadInst *&LI, Instruction *&Inst,
3613 const SmallVectorImpl<Instruction *> &Exts,
Quentin Colombet1b274f92015-03-10 21:48:15 +00003614 unsigned CreatedInstsCost = 0) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003615 // Iterate over all the extensions to see if one form an ext(load).
3616 for (auto I : Exts) {
3617 // Check if we directly have ext(load).
3618 if ((LI = dyn_cast<LoadInst>(I->getOperand(0)))) {
3619 Inst = I;
3620 // No promotion happened here.
3621 return false;
3622 }
3623 // Check whether or not we want to do any promotion.
3624 if (!TLI || !TLI->enableExtLdPromotion() || DisableExtLdPromotion)
3625 continue;
3626 // Get the action to perform the promotion.
3627 TypePromotionHelper::Action TPH = TypePromotionHelper::getAction(
3628 I, InsertedTruncsSet, *TLI, PromotedInsts);
3629 // Check if we can promote.
3630 if (!TPH)
3631 continue;
3632 // Save the current state.
3633 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3634 TPT.getRestorationPoint();
3635 SmallVector<Instruction *, 4> NewExts;
Quentin Colombet1b274f92015-03-10 21:48:15 +00003636 unsigned NewCreatedInstsCost = 0;
3637 unsigned ExtCost = !TLI->isExtFree(I);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003638 // Promote.
Quentin Colombet1b274f92015-03-10 21:48:15 +00003639 Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost,
3640 &NewExts, nullptr, *TLI);
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003641 assert(PromotedVal &&
3642 "TypePromotionHelper should have filtered out those cases");
3643
3644 // We would be able to merge only one extension in a load.
3645 // Therefore, if we have more than 1 new extension we heuristically
3646 // cut this search path, because it means we degrade the code quality.
3647 // With exactly 2, the transformation is neutral, because we will merge
3648 // one extension but leave one. However, we optimistically keep going,
3649 // because the new extension may be removed too.
Quentin Colombet1b274f92015-03-10 21:48:15 +00003650 long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost;
3651 TotalCreatedInstsCost -= ExtCost;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003652 if (!StressExtLdPromotion &&
Quentin Colombet1b274f92015-03-10 21:48:15 +00003653 (TotalCreatedInstsCost > 1 ||
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003654 !isPromotedInstructionLegal(*TLI, PromotedVal))) {
3655 // The promotion is not profitable, rollback to the previous state.
3656 TPT.rollback(LastKnownGood);
3657 continue;
3658 }
3659 // The promotion is profitable.
3660 // Check if it exposes an ext(load).
Quentin Colombet1b274f92015-03-10 21:48:15 +00003661 (void)ExtLdPromotion(TPT, LI, Inst, NewExts, TotalCreatedInstsCost);
3662 if (LI && (StressExtLdPromotion || NewCreatedInstsCost <= ExtCost ||
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003663 // If we have created a new extension, i.e., now we have two
3664 // extensions. We must make sure one of them is merged with
3665 // the load, otherwise we may degrade the code quality.
3666 (LI->hasOneUse() || hasSameExtUse(LI, *TLI))))
3667 // Promotion happened.
3668 return true;
3669 // If this does not help to expose an ext(load) then, rollback.
3670 TPT.rollback(LastKnownGood);
3671 }
3672 // None of the extension can form an ext(load).
3673 LI = nullptr;
3674 Inst = nullptr;
3675 return false;
3676}
3677
Dan Gohman99429a02009-10-16 20:59:35 +00003678/// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
3679/// basic block as the load, unless conditions are unfavorable. This allows
3680/// SelectionDAG to fold the extend into the load.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003681/// \p I[in/out] the extension may be modified during the process if some
3682/// promotions apply.
Dan Gohman99429a02009-10-16 20:59:35 +00003683///
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003684bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *&I) {
3685 // Try to promote a chain of computation if it allows to form
3686 // an extended load.
3687 TypePromotionTransaction TPT;
3688 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3689 TPT.getRestorationPoint();
3690 SmallVector<Instruction *, 1> Exts;
3691 Exts.push_back(I);
Dan Gohman99429a02009-10-16 20:59:35 +00003692 // Look for a load being extended.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003693 LoadInst *LI = nullptr;
3694 Instruction *OldExt = I;
3695 bool HasPromoted = ExtLdPromotion(TPT, LI, I, Exts);
3696 if (!LI || !I) {
3697 assert(!HasPromoted && !LI && "If we did not match any load instruction "
3698 "the code must remain the same");
3699 I = OldExt;
3700 return false;
3701 }
Dan Gohman99429a02009-10-16 20:59:35 +00003702
3703 // If they're already in the same block, there's nothing to do.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003704 // Make the cheap checks first if we did not promote.
3705 // If we promoted, we need to check if it is indeed profitable.
3706 if (!HasPromoted && LI->getParent() == I->getParent())
Dan Gohman99429a02009-10-16 20:59:35 +00003707 return false;
3708
Ahmed Bougacha55e3c2d2014-12-05 18:04:40 +00003709 EVT VT = TLI->getValueType(I->getType());
3710 EVT LoadVT = TLI->getValueType(LI->getType());
3711
Dan Gohman99429a02009-10-16 20:59:35 +00003712 // If the load has other users and the truncate is not free, this probably
3713 // isn't worthwhile.
Ahmed Bougacha55e3c2d2014-12-05 18:04:40 +00003714 if (!LI->hasOneUse() && TLI &&
3715 (TLI->isTypeLegal(LoadVT) || !TLI->isTypeLegal(VT)) &&
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003716 !TLI->isTruncateFree(I->getType(), LI->getType())) {
3717 I = OldExt;
3718 TPT.rollback(LastKnownGood);
Dan Gohman99429a02009-10-16 20:59:35 +00003719 return false;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003720 }
Dan Gohman99429a02009-10-16 20:59:35 +00003721
3722 // Check whether the target supports casts folded into loads.
3723 unsigned LType;
3724 if (isa<ZExtInst>(I))
3725 LType = ISD::ZEXTLOAD;
3726 else {
3727 assert(isa<SExtInst>(I) && "Unexpected ext type!");
3728 LType = ISD::SEXTLOAD;
3729 }
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00003730 if (TLI && !TLI->isLoadExtLegal(LType, VT, LoadVT)) {
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003731 I = OldExt;
3732 TPT.rollback(LastKnownGood);
Dan Gohman99429a02009-10-16 20:59:35 +00003733 return false;
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003734 }
Dan Gohman99429a02009-10-16 20:59:35 +00003735
3736 // Move the extend into the same block as the load, so that SelectionDAG
3737 // can fold it.
Quentin Colombetfc2201e2014-12-17 01:36:17 +00003738 TPT.commit();
Dan Gohman99429a02009-10-16 20:59:35 +00003739 I->removeFromParent();
3740 I->insertAfter(LI);
Cameron Zwarichced753f2011-01-05 17:27:27 +00003741 ++NumExtsMoved;
Dan Gohman99429a02009-10-16 20:59:35 +00003742 return true;
3743}
3744
Evan Chengd3d80172007-12-05 23:58:20 +00003745bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
3746 BasicBlock *DefBB = I->getParent();
3747
Bob Wilsonff714f92010-09-21 21:44:14 +00003748 // If the result of a {s|z}ext and its source are both live out, rewrite all
Evan Chengd3d80172007-12-05 23:58:20 +00003749 // other uses of the source with result of extension.
3750 Value *Src = I->getOperand(0);
3751 if (Src->hasOneUse())
3752 return false;
3753
Evan Cheng2011df42007-12-13 07:50:36 +00003754 // Only do this xform if truncating is free.
Gabor Greifaa261722008-02-26 19:13:21 +00003755 if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
Evan Cheng37c36ed2007-12-13 03:32:53 +00003756 return false;
3757
Evan Cheng7bc89422007-12-12 00:51:06 +00003758 // Only safe to perform the optimization if the source is also defined in
Evan Cheng63d33cf2007-12-12 02:53:41 +00003759 // this block.
3760 if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
Evan Cheng7bc89422007-12-12 00:51:06 +00003761 return false;
3762
Evan Chengd3d80172007-12-05 23:58:20 +00003763 bool DefIsLiveOut = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00003764 for (User *U : I->users()) {
3765 Instruction *UI = cast<Instruction>(U);
Evan Chengd3d80172007-12-05 23:58:20 +00003766
3767 // Figure out which BB this ext is used in.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003768 BasicBlock *UserBB = UI->getParent();
Evan Chengd3d80172007-12-05 23:58:20 +00003769 if (UserBB == DefBB) continue;
3770 DefIsLiveOut = true;
3771 break;
3772 }
3773 if (!DefIsLiveOut)
3774 return false;
3775
Jim Grosbach0f38c1e2013-04-15 17:40:48 +00003776 // Make sure none of the uses are PHI nodes.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003777 for (User *U : Src->users()) {
3778 Instruction *UI = cast<Instruction>(U);
3779 BasicBlock *UserBB = UI->getParent();
Evan Cheng37c36ed2007-12-13 03:32:53 +00003780 if (UserBB == DefBB) continue;
3781 // Be conservative. We don't want this xform to end up introducing
3782 // reloads just before load / store instructions.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003783 if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI))
Evan Cheng63d33cf2007-12-12 02:53:41 +00003784 return false;
3785 }
3786
Evan Chengd3d80172007-12-05 23:58:20 +00003787 // InsertedTruncs - Only insert one trunc in each block once.
3788 DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
3789
3790 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00003791 for (Use &U : Src->uses()) {
3792 Instruction *User = cast<Instruction>(U.getUser());
Evan Chengd3d80172007-12-05 23:58:20 +00003793
3794 // Figure out which BB this ext is used in.
3795 BasicBlock *UserBB = User->getParent();
3796 if (UserBB == DefBB) continue;
3797
3798 // Both src and def are live in this block. Rewrite the use.
3799 Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
3800
3801 if (!InsertedTrunc) {
Bill Wendling8ddfc092011-08-16 20:45:24 +00003802 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
Evan Chengd3d80172007-12-05 23:58:20 +00003803 InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
Quentin Colombet3a4bf042014-02-06 21:44:56 +00003804 InsertedTruncsSet.insert(InsertedTrunc);
Evan Chengd3d80172007-12-05 23:58:20 +00003805 }
3806
3807 // Replace a use of the {s|z}ext source with a use of the result.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003808 U = InsertedTrunc;
Cameron Zwarichced753f2011-01-05 17:27:27 +00003809 ++NumExtUses;
Evan Chengd3d80172007-12-05 23:58:20 +00003810 MadeChange = true;
3811 }
3812
3813 return MadeChange;
3814}
3815
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00003816/// isFormingBranchFromSelectProfitable - Returns true if a SelectInst should be
3817/// turned into an explicit branch.
3818static bool isFormingBranchFromSelectProfitable(SelectInst *SI) {
3819 // FIXME: This should use the same heuristics as IfConversion to determine
3820 // whether a select is better represented as a branch. This requires that
3821 // branch probability metadata is preserved for the select, which is not the
3822 // case currently.
3823
3824 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
3825
3826 // If the branch is predicted right, an out of order CPU can avoid blocking on
3827 // the compare. Emit cmovs on compares with a memory operand as branches to
3828 // avoid stalls on the load from memory. If the compare has more than one use
3829 // there's probably another cmov or setcc around so it's not worth emitting a
3830 // branch.
3831 if (!Cmp)
3832 return false;
3833
3834 Value *CmpOp0 = Cmp->getOperand(0);
3835 Value *CmpOp1 = Cmp->getOperand(1);
3836
3837 // We check that the memory operand has one use to avoid uses of the loaded
3838 // value directly after the compare, making branches unprofitable.
3839 return Cmp->hasOneUse() &&
3840 ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) ||
3841 (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse()));
3842}
3843
3844
Nadav Rotem9d832022012-09-02 12:10:19 +00003845/// If we have a SelectInst that will likely profit from branch prediction,
3846/// turn it into a branch.
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00003847bool CodeGenPrepare::OptimizeSelectInst(SelectInst *SI) {
Nadav Rotem9d832022012-09-02 12:10:19 +00003848 bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1);
3849
3850 // Can we convert the 'select' to CF ?
3851 if (DisableSelectToBranch || OptSize || !TLI || VectorCond)
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00003852 return false;
3853
Nadav Rotem9d832022012-09-02 12:10:19 +00003854 TargetLowering::SelectSupportKind SelectKind;
3855 if (VectorCond)
3856 SelectKind = TargetLowering::VectorMaskSelect;
3857 else if (SI->getType()->isVectorTy())
3858 SelectKind = TargetLowering::ScalarCondVectorVal;
3859 else
3860 SelectKind = TargetLowering::ScalarValSelect;
3861
3862 // Do we have efficient codegen support for this kind of 'selects' ?
3863 if (TLI->isSelectSupported(SelectKind)) {
3864 // We have efficient codegen support for the select instruction.
3865 // Check if it is profitable to keep this 'select'.
3866 if (!TLI->isPredictableSelectExpensive() ||
3867 !isFormingBranchFromSelectProfitable(SI))
3868 return false;
3869 }
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00003870
3871 ModifiedDT = true;
3872
3873 // First, we split the block containing the select into 2 blocks.
3874 BasicBlock *StartBlock = SI->getParent();
3875 BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI));
3876 BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end");
3877
3878 // Create a new block serving as the landing pad for the branch.
3879 BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid",
3880 NextBlock->getParent(), NextBlock);
3881
3882 // Move the unconditional branch from the block with the select in it into our
3883 // landing pad block.
3884 StartBlock->getTerminator()->eraseFromParent();
3885 BranchInst::Create(NextBlock, SmallBlock);
3886
3887 // Insert the real conditional branch based on the original condition.
3888 BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI);
3889
3890 // The select itself is replaced with a PHI Node.
3891 PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin());
3892 PN->takeName(SI);
3893 PN->addIncoming(SI->getTrueValue(), StartBlock);
3894 PN->addIncoming(SI->getFalseValue(), SmallBlock);
3895 SI->replaceAllUsesWith(PN);
3896 SI->eraseFromParent();
3897
3898 // Instruct OptimizeBlock to skip to the next block.
3899 CurInstIterator = StartBlock->end();
3900 ++NumSelectsExpanded;
3901 return true;
3902}
3903
Benjamin Kramer573ff362014-03-01 17:24:40 +00003904static bool isBroadcastShuffle(ShuffleVectorInst *SVI) {
Tim Northoveraeb8e062014-02-19 10:02:43 +00003905 SmallVector<int, 16> Mask(SVI->getShuffleMask());
3906 int SplatElem = -1;
3907 for (unsigned i = 0; i < Mask.size(); ++i) {
3908 if (SplatElem != -1 && Mask[i] != -1 && Mask[i] != SplatElem)
3909 return false;
3910 SplatElem = Mask[i];
3911 }
3912
3913 return true;
3914}
3915
3916/// Some targets have expensive vector shifts if the lanes aren't all the same
3917/// (e.g. x86 only introduced "vpsllvd" and friends with AVX2). In these cases
3918/// it's often worth sinking a shufflevector splat down to its use so that
3919/// codegen can spot all lanes are identical.
3920bool CodeGenPrepare::OptimizeShuffleVectorInst(ShuffleVectorInst *SVI) {
3921 BasicBlock *DefBB = SVI->getParent();
3922
3923 // Only do this xform if variable vector shifts are particularly expensive.
3924 if (!TLI || !TLI->isVectorShiftByScalarCheap(SVI->getType()))
3925 return false;
3926
3927 // We only expect better codegen by sinking a shuffle if we can recognise a
3928 // constant splat.
3929 if (!isBroadcastShuffle(SVI))
3930 return false;
3931
3932 // InsertedShuffles - Only insert a shuffle in each block once.
3933 DenseMap<BasicBlock*, Instruction*> InsertedShuffles;
3934
3935 bool MadeChange = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +00003936 for (User *U : SVI->users()) {
3937 Instruction *UI = cast<Instruction>(U);
Tim Northoveraeb8e062014-02-19 10:02:43 +00003938
3939 // Figure out which BB this ext is used in.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003940 BasicBlock *UserBB = UI->getParent();
Tim Northoveraeb8e062014-02-19 10:02:43 +00003941 if (UserBB == DefBB) continue;
3942
3943 // For now only apply this when the splat is used by a shift instruction.
Chandler Carruthcdf47882014-03-09 03:16:01 +00003944 if (!UI->isShift()) continue;
Tim Northoveraeb8e062014-02-19 10:02:43 +00003945
3946 // Everything checks out, sink the shuffle if the user's block doesn't
3947 // already have a copy.
3948 Instruction *&InsertedShuffle = InsertedShuffles[UserBB];
3949
3950 if (!InsertedShuffle) {
3951 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
3952 InsertedShuffle = new ShuffleVectorInst(SVI->getOperand(0),
3953 SVI->getOperand(1),
3954 SVI->getOperand(2), "", InsertPt);
3955 }
3956
Chandler Carruthcdf47882014-03-09 03:16:01 +00003957 UI->replaceUsesOfWith(SVI, InsertedShuffle);
Tim Northoveraeb8e062014-02-19 10:02:43 +00003958 MadeChange = true;
3959 }
3960
3961 // If we removed all uses, nuke the shuffle.
3962 if (SVI->use_empty()) {
3963 SVI->eraseFromParent();
3964 MadeChange = true;
3965 }
3966
3967 return MadeChange;
3968}
3969
Quentin Colombetc32615d2014-10-31 17:52:53 +00003970namespace {
3971/// \brief Helper class to promote a scalar operation to a vector one.
3972/// This class is used to move downward extractelement transition.
3973/// E.g.,
3974/// a = vector_op <2 x i32>
3975/// b = extractelement <2 x i32> a, i32 0
3976/// c = scalar_op b
3977/// store c
3978///
3979/// =>
3980/// a = vector_op <2 x i32>
3981/// c = vector_op a (equivalent to scalar_op on the related lane)
3982/// * d = extractelement <2 x i32> c, i32 0
3983/// * store d
3984/// Assuming both extractelement and store can be combine, we get rid of the
3985/// transition.
3986class VectorPromoteHelper {
3987 /// Used to perform some checks on the legality of vector operations.
3988 const TargetLowering &TLI;
3989
3990 /// Used to estimated the cost of the promoted chain.
3991 const TargetTransformInfo &TTI;
3992
3993 /// The transition being moved downwards.
3994 Instruction *Transition;
3995 /// The sequence of instructions to be promoted.
3996 SmallVector<Instruction *, 4> InstsToBePromoted;
3997 /// Cost of combining a store and an extract.
3998 unsigned StoreExtractCombineCost;
3999 /// Instruction that will be combined with the transition.
4000 Instruction *CombineInst;
4001
4002 /// \brief The instruction that represents the current end of the transition.
4003 /// Since we are faking the promotion until we reach the end of the chain
4004 /// of computation, we need a way to get the current end of the transition.
4005 Instruction *getEndOfTransition() const {
4006 if (InstsToBePromoted.empty())
4007 return Transition;
4008 return InstsToBePromoted.back();
4009 }
4010
4011 /// \brief Return the index of the original value in the transition.
4012 /// E.g., for "extractelement <2 x i32> c, i32 1" the original value,
4013 /// c, is at index 0.
4014 unsigned getTransitionOriginalValueIdx() const {
4015 assert(isa<ExtractElementInst>(Transition) &&
4016 "Other kind of transitions are not supported yet");
4017 return 0;
4018 }
4019
4020 /// \brief Return the index of the index in the transition.
4021 /// E.g., for "extractelement <2 x i32> c, i32 0" the index
4022 /// is at index 1.
4023 unsigned getTransitionIdx() const {
4024 assert(isa<ExtractElementInst>(Transition) &&
4025 "Other kind of transitions are not supported yet");
4026 return 1;
4027 }
4028
4029 /// \brief Get the type of the transition.
4030 /// This is the type of the original value.
4031 /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the
4032 /// transition is <2 x i32>.
4033 Type *getTransitionType() const {
4034 return Transition->getOperand(getTransitionOriginalValueIdx())->getType();
4035 }
4036
4037 /// \brief Promote \p ToBePromoted by moving \p Def downward through.
4038 /// I.e., we have the following sequence:
4039 /// Def = Transition <ty1> a to <ty2>
4040 /// b = ToBePromoted <ty2> Def, ...
4041 /// =>
4042 /// b = ToBePromoted <ty1> a, ...
4043 /// Def = Transition <ty1> ToBePromoted to <ty2>
4044 void promoteImpl(Instruction *ToBePromoted);
4045
4046 /// \brief Check whether or not it is profitable to promote all the
4047 /// instructions enqueued to be promoted.
4048 bool isProfitableToPromote() {
4049 Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx());
4050 unsigned Index = isa<ConstantInt>(ValIdx)
4051 ? cast<ConstantInt>(ValIdx)->getZExtValue()
4052 : -1;
4053 Type *PromotedType = getTransitionType();
4054
4055 StoreInst *ST = cast<StoreInst>(CombineInst);
4056 unsigned AS = ST->getPointerAddressSpace();
4057 unsigned Align = ST->getAlignment();
4058 // Check if this store is supported.
4059 if (!TLI.allowsMisalignedMemoryAccesses(
Ahmed Bougacha026600d2014-11-12 23:05:03 +00004060 TLI.getValueType(ST->getValueOperand()->getType()), AS, Align)) {
Quentin Colombetc32615d2014-10-31 17:52:53 +00004061 // If this is not supported, there is no way we can combine
4062 // the extract with the store.
4063 return false;
4064 }
4065
4066 // The scalar chain of computation has to pay for the transition
4067 // scalar to vector.
4068 // The vector chain has to account for the combining cost.
4069 uint64_t ScalarCost =
4070 TTI.getVectorInstrCost(Transition->getOpcode(), PromotedType, Index);
4071 uint64_t VectorCost = StoreExtractCombineCost;
4072 for (const auto &Inst : InstsToBePromoted) {
4073 // Compute the cost.
4074 // By construction, all instructions being promoted are arithmetic ones.
4075 // Moreover, one argument is a constant that can be viewed as a splat
4076 // constant.
4077 Value *Arg0 = Inst->getOperand(0);
4078 bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) ||
4079 isa<ConstantFP>(Arg0);
4080 TargetTransformInfo::OperandValueKind Arg0OVK =
4081 IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
4082 : TargetTransformInfo::OK_AnyValue;
4083 TargetTransformInfo::OperandValueKind Arg1OVK =
4084 !IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
4085 : TargetTransformInfo::OK_AnyValue;
4086 ScalarCost += TTI.getArithmeticInstrCost(
4087 Inst->getOpcode(), Inst->getType(), Arg0OVK, Arg1OVK);
4088 VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType,
4089 Arg0OVK, Arg1OVK);
4090 }
4091 DEBUG(dbgs() << "Estimated cost of computation to be promoted:\nScalar: "
4092 << ScalarCost << "\nVector: " << VectorCost << '\n');
4093 return ScalarCost > VectorCost;
4094 }
4095
4096 /// \brief Generate a constant vector with \p Val with the same
4097 /// number of elements as the transition.
4098 /// \p UseSplat defines whether or not \p Val should be replicated
4099 /// accross the whole vector.
4100 /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>,
4101 /// otherwise we generate a vector with as many undef as possible:
4102 /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only
4103 /// used at the index of the extract.
4104 Value *getConstantVector(Constant *Val, bool UseSplat) const {
4105 unsigned ExtractIdx = UINT_MAX;
4106 if (!UseSplat) {
4107 // If we cannot determine where the constant must be, we have to
4108 // use a splat constant.
4109 Value *ValExtractIdx = Transition->getOperand(getTransitionIdx());
4110 if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx))
4111 ExtractIdx = CstVal->getSExtValue();
4112 else
4113 UseSplat = true;
4114 }
4115
4116 unsigned End = getTransitionType()->getVectorNumElements();
4117 if (UseSplat)
4118 return ConstantVector::getSplat(End, Val);
4119
4120 SmallVector<Constant *, 4> ConstVec;
4121 UndefValue *UndefVal = UndefValue::get(Val->getType());
4122 for (unsigned Idx = 0; Idx != End; ++Idx) {
4123 if (Idx == ExtractIdx)
4124 ConstVec.push_back(Val);
4125 else
4126 ConstVec.push_back(UndefVal);
4127 }
4128 return ConstantVector::get(ConstVec);
4129 }
4130
4131 /// \brief Check if promoting to a vector type an operand at \p OperandIdx
4132 /// in \p Use can trigger undefined behavior.
4133 static bool canCauseUndefinedBehavior(const Instruction *Use,
4134 unsigned OperandIdx) {
4135 // This is not safe to introduce undef when the operand is on
4136 // the right hand side of a division-like instruction.
4137 if (OperandIdx != 1)
4138 return false;
4139 switch (Use->getOpcode()) {
4140 default:
4141 return false;
4142 case Instruction::SDiv:
4143 case Instruction::UDiv:
4144 case Instruction::SRem:
4145 case Instruction::URem:
4146 return true;
4147 case Instruction::FDiv:
4148 case Instruction::FRem:
4149 return !Use->hasNoNaNs();
4150 }
4151 llvm_unreachable(nullptr);
4152 }
4153
4154public:
4155 VectorPromoteHelper(const TargetLowering &TLI, const TargetTransformInfo &TTI,
4156 Instruction *Transition, unsigned CombineCost)
4157 : TLI(TLI), TTI(TTI), Transition(Transition),
4158 StoreExtractCombineCost(CombineCost), CombineInst(nullptr) {
4159 assert(Transition && "Do not know how to promote null");
4160 }
4161
4162 /// \brief Check if we can promote \p ToBePromoted to \p Type.
4163 bool canPromote(const Instruction *ToBePromoted) const {
4164 // We could support CastInst too.
4165 return isa<BinaryOperator>(ToBePromoted);
4166 }
4167
4168 /// \brief Check if it is profitable to promote \p ToBePromoted
4169 /// by moving downward the transition through.
4170 bool shouldPromote(const Instruction *ToBePromoted) const {
4171 // Promote only if all the operands can be statically expanded.
4172 // Indeed, we do not want to introduce any new kind of transitions.
4173 for (const Use &U : ToBePromoted->operands()) {
4174 const Value *Val = U.get();
4175 if (Val == getEndOfTransition()) {
4176 // If the use is a division and the transition is on the rhs,
4177 // we cannot promote the operation, otherwise we may create a
4178 // division by zero.
4179 if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()))
4180 return false;
4181 continue;
4182 }
4183 if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) &&
4184 !isa<ConstantFP>(Val))
4185 return false;
4186 }
4187 // Check that the resulting operation is legal.
4188 int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode());
4189 if (!ISDOpcode)
4190 return false;
4191 return StressStoreExtract ||
Ahmed Bougacha026600d2014-11-12 23:05:03 +00004192 TLI.isOperationLegalOrCustom(
4193 ISDOpcode, TLI.getValueType(getTransitionType(), true));
Quentin Colombetc32615d2014-10-31 17:52:53 +00004194 }
4195
4196 /// \brief Check whether or not \p Use can be combined
4197 /// with the transition.
4198 /// I.e., is it possible to do Use(Transition) => AnotherUse?
4199 bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); }
4200
4201 /// \brief Record \p ToBePromoted as part of the chain to be promoted.
4202 void enqueueForPromotion(Instruction *ToBePromoted) {
4203 InstsToBePromoted.push_back(ToBePromoted);
4204 }
4205
4206 /// \brief Set the instruction that will be combined with the transition.
4207 void recordCombineInstruction(Instruction *ToBeCombined) {
4208 assert(canCombine(ToBeCombined) && "Unsupported instruction to combine");
4209 CombineInst = ToBeCombined;
4210 }
4211
4212 /// \brief Promote all the instructions enqueued for promotion if it is
4213 /// is profitable.
4214 /// \return True if the promotion happened, false otherwise.
4215 bool promote() {
4216 // Check if there is something to promote.
4217 // Right now, if we do not have anything to combine with,
4218 // we assume the promotion is not profitable.
4219 if (InstsToBePromoted.empty() || !CombineInst)
4220 return false;
4221
4222 // Check cost.
4223 if (!StressStoreExtract && !isProfitableToPromote())
4224 return false;
4225
4226 // Promote.
4227 for (auto &ToBePromoted : InstsToBePromoted)
4228 promoteImpl(ToBePromoted);
4229 InstsToBePromoted.clear();
4230 return true;
4231 }
4232};
4233} // End of anonymous namespace.
4234
4235void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) {
4236 // At this point, we know that all the operands of ToBePromoted but Def
4237 // can be statically promoted.
4238 // For Def, we need to use its parameter in ToBePromoted:
4239 // b = ToBePromoted ty1 a
4240 // Def = Transition ty1 b to ty2
4241 // Move the transition down.
4242 // 1. Replace all uses of the promoted operation by the transition.
4243 // = ... b => = ... Def.
4244 assert(ToBePromoted->getType() == Transition->getType() &&
4245 "The type of the result of the transition does not match "
4246 "the final type");
4247 ToBePromoted->replaceAllUsesWith(Transition);
4248 // 2. Update the type of the uses.
4249 // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def.
4250 Type *TransitionTy = getTransitionType();
4251 ToBePromoted->mutateType(TransitionTy);
4252 // 3. Update all the operands of the promoted operation with promoted
4253 // operands.
4254 // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a.
4255 for (Use &U : ToBePromoted->operands()) {
4256 Value *Val = U.get();
4257 Value *NewVal = nullptr;
4258 if (Val == Transition)
4259 NewVal = Transition->getOperand(getTransitionOriginalValueIdx());
4260 else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) ||
4261 isa<ConstantFP>(Val)) {
4262 // Use a splat constant if it is not safe to use undef.
4263 NewVal = getConstantVector(
4264 cast<Constant>(Val),
4265 isa<UndefValue>(Val) ||
4266 canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()));
4267 } else
Craig Topperd3c02f12015-01-05 10:15:49 +00004268 llvm_unreachable("Did you modified shouldPromote and forgot to update "
4269 "this?");
Quentin Colombetc32615d2014-10-31 17:52:53 +00004270 ToBePromoted->setOperand(U.getOperandNo(), NewVal);
4271 }
4272 Transition->removeFromParent();
4273 Transition->insertAfter(ToBePromoted);
4274 Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted);
4275}
4276
4277/// Some targets can do store(extractelement) with one instruction.
4278/// Try to push the extractelement towards the stores when the target
4279/// has this feature and this is profitable.
4280bool CodeGenPrepare::OptimizeExtractElementInst(Instruction *Inst) {
4281 unsigned CombineCost = UINT_MAX;
4282 if (DisableStoreExtract || !TLI ||
4283 (!StressStoreExtract &&
4284 !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(),
4285 Inst->getOperand(1), CombineCost)))
4286 return false;
4287
4288 // At this point we know that Inst is a vector to scalar transition.
4289 // Try to move it down the def-use chain, until:
4290 // - We can combine the transition with its single use
4291 // => we got rid of the transition.
4292 // - We escape the current basic block
4293 // => we would need to check that we are moving it at a cheaper place and
4294 // we do not do that for now.
4295 BasicBlock *Parent = Inst->getParent();
4296 DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n');
4297 VectorPromoteHelper VPH(*TLI, *TTI, Inst, CombineCost);
4298 // If the transition has more than one use, assume this is not going to be
4299 // beneficial.
4300 while (Inst->hasOneUse()) {
4301 Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin());
4302 DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n');
4303
4304 if (ToBePromoted->getParent() != Parent) {
4305 DEBUG(dbgs() << "Instruction to promote is in a different block ("
4306 << ToBePromoted->getParent()->getName()
4307 << ") than the transition (" << Parent->getName() << ").\n");
4308 return false;
4309 }
4310
4311 if (VPH.canCombine(ToBePromoted)) {
4312 DEBUG(dbgs() << "Assume " << *Inst << '\n'
4313 << "will be combined with: " << *ToBePromoted << '\n');
4314 VPH.recordCombineInstruction(ToBePromoted);
4315 bool Changed = VPH.promote();
4316 NumStoreExtractExposed += Changed;
4317 return Changed;
4318 }
4319
4320 DEBUG(dbgs() << "Try promoting.\n");
4321 if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted))
4322 return false;
4323
4324 DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n");
4325
4326 VPH.enqueueForPromotion(ToBePromoted);
4327 Inst = ToBePromoted;
4328 }
4329 return false;
4330}
4331
Elena Demikhovsky87700a72014-12-28 08:54:45 +00004332bool CodeGenPrepare::OptimizeInst(Instruction *I, bool& ModifiedDT) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004333 if (PHINode *P = dyn_cast<PHINode>(I)) {
4334 // It is possible for very late stage optimizations (such as SimplifyCFG)
4335 // to introduce PHI nodes too late to be cleaned up. If we detect such a
4336 // trivial PHI, go ahead and zap it here.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00004337 const DataLayout &DL = I->getModule()->getDataLayout();
Quentin Colombet7bdd50d2015-03-18 23:17:28 +00004338 if (Value *V = SimplifyInstruction(P, DL, TLInfo, nullptr)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004339 P->replaceAllUsesWith(V);
4340 P->eraseFromParent();
4341 ++NumPHIsElim;
Chris Lattneree588de2011-01-15 07:29:01 +00004342 return true;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004343 }
Chris Lattneree588de2011-01-15 07:29:01 +00004344 return false;
4345 }
Nadav Rotem465834c2012-07-24 10:51:42 +00004346
Chris Lattneree588de2011-01-15 07:29:01 +00004347 if (CastInst *CI = dyn_cast<CastInst>(I)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004348 // If the source of the cast is a constant, then this should have
4349 // already been constant folded. The only reason NOT to constant fold
4350 // it is if something (e.g. LSR) was careful to place the constant
4351 // evaluation in a block other than then one that uses it (e.g. to hoist
4352 // the address of globals out of a loop). If this is the case, we don't
4353 // want to forward-subst the cast.
4354 if (isa<Constant>(CI->getOperand(0)))
4355 return false;
4356
Chris Lattneree588de2011-01-15 07:29:01 +00004357 if (TLI && OptimizeNoopCopyExpression(CI, *TLI))
4358 return true;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004359
Chris Lattneree588de2011-01-15 07:29:01 +00004360 if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
Manuel Jacoba7c48f92014-03-13 13:36:25 +00004361 /// Sink a zext or sext into its user blocks if the target type doesn't
4362 /// fit in one register
4363 if (TLI && TLI->getTypeAction(CI->getContext(),
4364 TLI->getValueType(CI->getType())) ==
4365 TargetLowering::TypeExpandInteger) {
4366 return SinkCast(CI);
4367 } else {
4368 bool MadeChange = MoveExtToFormExtLoad(I);
4369 return MadeChange | OptimizeExtUses(I);
4370 }
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004371 }
Chris Lattneree588de2011-01-15 07:29:01 +00004372 return false;
4373 }
Nadav Rotem465834c2012-07-24 10:51:42 +00004374
Chris Lattneree588de2011-01-15 07:29:01 +00004375 if (CmpInst *CI = dyn_cast<CmpInst>(I))
Hal Finkeldecb0242014-01-02 21:13:43 +00004376 if (!TLI || !TLI->hasMultipleConditionRegisters())
4377 return OptimizeCmpExpression(CI);
Nadav Rotem465834c2012-07-24 10:51:42 +00004378
Chris Lattneree588de2011-01-15 07:29:01 +00004379 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004380 if (TLI)
Hans Wennborgf3254832012-10-30 11:23:25 +00004381 return OptimizeMemoryInst(I, I->getOperand(0), LI->getType());
4382 return false;
Chris Lattneree588de2011-01-15 07:29:01 +00004383 }
Nadav Rotem465834c2012-07-24 10:51:42 +00004384
Chris Lattneree588de2011-01-15 07:29:01 +00004385 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004386 if (TLI)
Chris Lattneree588de2011-01-15 07:29:01 +00004387 return OptimizeMemoryInst(I, SI->getOperand(1),
4388 SI->getOperand(0)->getType());
4389 return false;
4390 }
Nadav Rotem465834c2012-07-24 10:51:42 +00004391
Yi Jiangd069f632014-04-21 19:34:27 +00004392 BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I);
4393
4394 if (BinOp && (BinOp->getOpcode() == Instruction::AShr ||
4395 BinOp->getOpcode() == Instruction::LShr)) {
4396 ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1));
4397 if (TLI && CI && TLI->hasExtractBitsInsn())
4398 return OptimizeExtractBits(BinOp, CI, *TLI);
4399
4400 return false;
4401 }
4402
Chris Lattneree588de2011-01-15 07:29:01 +00004403 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
Cameron Zwarichd28c78e2011-01-06 02:44:52 +00004404 if (GEPI->hasAllZeroIndices()) {
4405 /// The GEP operand must be a pointer, so must its result -> BitCast
4406 Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
4407 GEPI->getName(), GEPI);
4408 GEPI->replaceAllUsesWith(NC);
4409 GEPI->eraseFromParent();
4410 ++NumGEPsElim;
Elena Demikhovsky87700a72014-12-28 08:54:45 +00004411 OptimizeInst(NC, ModifiedDT);
Chris Lattneree588de2011-01-15 07:29:01 +00004412 return true;
Cameron Zwarichd28c78e2011-01-06 02:44:52 +00004413 }
Chris Lattneree588de2011-01-15 07:29:01 +00004414 return false;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004415 }
Nadav Rotem465834c2012-07-24 10:51:42 +00004416
Chris Lattneree588de2011-01-15 07:29:01 +00004417 if (CallInst *CI = dyn_cast<CallInst>(I))
Elena Demikhovsky87700a72014-12-28 08:54:45 +00004418 return OptimizeCallInst(CI, ModifiedDT);
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004419
Benjamin Kramer047d7ca2012-05-05 12:49:22 +00004420 if (SelectInst *SI = dyn_cast<SelectInst>(I))
4421 return OptimizeSelectInst(SI);
4422
Tim Northoveraeb8e062014-02-19 10:02:43 +00004423 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I))
4424 return OptimizeShuffleVectorInst(SVI);
4425
Quentin Colombetc32615d2014-10-31 17:52:53 +00004426 if (isa<ExtractElementInst>(I))
4427 return OptimizeExtractElementInst(I);
4428
Chris Lattneree588de2011-01-15 07:29:01 +00004429 return false;
Cameron Zwarich14ac8652011-01-06 02:37:26 +00004430}
4431
Chris Lattnerf2836d12007-03-31 04:06:36 +00004432// In this pass we look for GEP and cast instructions that are used
4433// across basic blocks and rewrite them to improve basic-block-at-a-time
4434// selection.
Elena Demikhovsky87700a72014-12-28 08:54:45 +00004435bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB, bool& ModifiedDT) {
Cameron Zwarichce3b9302011-01-06 00:42:50 +00004436 SunkAddrs.clear();
Cameron Zwarich5dd2aa22011-03-02 03:31:46 +00004437 bool MadeChange = false;
Eric Christopherc1ea1492008-09-24 05:32:41 +00004438
Chris Lattner7a277142011-01-15 07:14:54 +00004439 CurInstIterator = BB.begin();
Elena Demikhovsky87700a72014-12-28 08:54:45 +00004440 while (CurInstIterator != BB.end()) {
4441 MadeChange |= OptimizeInst(CurInstIterator++, ModifiedDT);
4442 if (ModifiedDT)
4443 return true;
4444 }
Benjamin Kramer455fa352012-11-23 19:17:06 +00004445 MadeChange |= DupRetToEnableTailCallOpts(&BB);
4446
Chris Lattnerf2836d12007-03-31 04:06:36 +00004447 return MadeChange;
4448}
Devang Patel53771ba2011-08-18 00:50:51 +00004449
4450// llvm.dbg.value is far away from the value then iSel may not be able
Nadav Rotem465834c2012-07-24 10:51:42 +00004451// handle it properly. iSel will drop llvm.dbg.value if it can not
Devang Patel53771ba2011-08-18 00:50:51 +00004452// find a node corresponding to the value.
4453bool CodeGenPrepare::PlaceDbgValues(Function &F) {
4454 bool MadeChange = false;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +00004455 for (BasicBlock &BB : F) {
Craig Topperc0196b12014-04-14 00:51:57 +00004456 Instruction *PrevNonDbgInst = nullptr;
Duncan P. N. Exon Smith5914a972015-01-08 20:44:33 +00004457 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
Duncan P. N. Exon Smithe90f1162015-01-08 21:07:55 +00004458 Instruction *Insn = BI++;
Devang Patel53771ba2011-08-18 00:50:51 +00004459 DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn);
Adrian Prantl32da8892014-04-25 20:49:25 +00004460 // Leave dbg.values that refer to an alloca alone. These
4461 // instrinsics describe the address of a variable (= the alloca)
4462 // being taken. They should not be moved next to the alloca
4463 // (and to the beginning of the scope), but rather stay close to
4464 // where said address is used.
4465 if (!DVI || (DVI->getValue() && isa<AllocaInst>(DVI->getValue()))) {
Devang Patel53771ba2011-08-18 00:50:51 +00004466 PrevNonDbgInst = Insn;
4467 continue;
4468 }
4469
4470 Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue());
4471 if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) {
4472 DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI);
4473 DVI->removeFromParent();
4474 if (isa<PHINode>(VI))
4475 DVI->insertBefore(VI->getParent()->getFirstInsertionPt());
4476 else
4477 DVI->insertAfter(VI);
4478 MadeChange = true;
4479 ++NumDbgValueMoved;
4480 }
4481 }
4482 }
4483 return MadeChange;
4484}
Tim Northovercea0abb2014-03-29 08:22:29 +00004485
4486// If there is a sequence that branches based on comparing a single bit
4487// against zero that can be combined into a single instruction, and the
4488// target supports folding these into a single instruction, sink the
4489// mask and compare into the branch uses. Do this before OptimizeBlock ->
4490// OptimizeInst -> OptimizeCmpExpression, which perturbs the pattern being
4491// searched for.
4492bool CodeGenPrepare::sinkAndCmp(Function &F) {
4493 if (!EnableAndCmpSinking)
4494 return false;
4495 if (!TLI || !TLI->isMaskAndBranchFoldingLegal())
4496 return false;
4497 bool MadeChange = false;
4498 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
4499 BasicBlock *BB = I++;
4500
4501 // Does this BB end with the following?
4502 // %andVal = and %val, #single-bit-set
4503 // %icmpVal = icmp %andResult, 0
4504 // br i1 %cmpVal label %dest1, label %dest2"
4505 BranchInst *Brcc = dyn_cast<BranchInst>(BB->getTerminator());
4506 if (!Brcc || !Brcc->isConditional())
4507 continue;
4508 ICmpInst *Cmp = dyn_cast<ICmpInst>(Brcc->getOperand(0));
4509 if (!Cmp || Cmp->getParent() != BB)
4510 continue;
4511 ConstantInt *Zero = dyn_cast<ConstantInt>(Cmp->getOperand(1));
4512 if (!Zero || !Zero->isZero())
4513 continue;
4514 Instruction *And = dyn_cast<Instruction>(Cmp->getOperand(0));
4515 if (!And || And->getOpcode() != Instruction::And || And->getParent() != BB)
4516 continue;
4517 ConstantInt* Mask = dyn_cast<ConstantInt>(And->getOperand(1));
4518 if (!Mask || !Mask->getUniqueInteger().isPowerOf2())
4519 continue;
4520 DEBUG(dbgs() << "found and; icmp ?,0; brcc\n"); DEBUG(BB->dump());
4521
4522 // Push the "and; icmp" for any users that are conditional branches.
4523 // Since there can only be one branch use per BB, we don't need to keep
4524 // track of which BBs we insert into.
4525 for (Value::use_iterator UI = Cmp->use_begin(), E = Cmp->use_end();
4526 UI != E; ) {
4527 Use &TheUse = *UI;
4528 // Find brcc use.
4529 BranchInst *BrccUser = dyn_cast<BranchInst>(*UI);
4530 ++UI;
4531 if (!BrccUser || !BrccUser->isConditional())
4532 continue;
4533 BasicBlock *UserBB = BrccUser->getParent();
4534 if (UserBB == BB) continue;
4535 DEBUG(dbgs() << "found Brcc use\n");
4536
4537 // Sink the "and; icmp" to use.
4538 MadeChange = true;
4539 BinaryOperator *NewAnd =
4540 BinaryOperator::CreateAnd(And->getOperand(0), And->getOperand(1), "",
4541 BrccUser);
4542 CmpInst *NewCmp =
4543 CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(), NewAnd, Zero,
4544 "", BrccUser);
4545 TheUse = NewCmp;
4546 ++NumAndCmpsMoved;
4547 DEBUG(BrccUser->getParent()->dump());
4548 }
4549 }
4550 return MadeChange;
4551}
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004552
Juergen Ributzka194350a2014-12-09 17:32:12 +00004553/// \brief Retrieve the probabilities of a conditional branch. Returns true on
4554/// success, or returns false if no or invalid metadata was found.
4555static bool extractBranchMetadata(BranchInst *BI,
4556 uint64_t &ProbTrue, uint64_t &ProbFalse) {
4557 assert(BI->isConditional() &&
4558 "Looking for probabilities on unconditional branch?");
4559 auto *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
4560 if (!ProfileData || ProfileData->getNumOperands() != 3)
4561 return false;
4562
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004563 const auto *CITrue =
4564 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
4565 const auto *CIFalse =
4566 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
Juergen Ributzka194350a2014-12-09 17:32:12 +00004567 if (!CITrue || !CIFalse)
4568 return false;
4569
4570 ProbTrue = CITrue->getValue().getZExtValue();
4571 ProbFalse = CIFalse->getValue().getZExtValue();
4572
4573 return true;
4574}
4575
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004576/// \brief Scale down both weights to fit into uint32_t.
4577static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) {
4578 uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
4579 uint32_t Scale = (NewMax / UINT32_MAX) + 1;
4580 NewTrue = NewTrue / Scale;
4581 NewFalse = NewFalse / Scale;
4582}
4583
4584/// \brief Some targets prefer to split a conditional branch like:
4585/// \code
4586/// %0 = icmp ne i32 %a, 0
4587/// %1 = icmp ne i32 %b, 0
4588/// %or.cond = or i1 %0, %1
4589/// br i1 %or.cond, label %TrueBB, label %FalseBB
4590/// \endcode
4591/// into multiple branch instructions like:
4592/// \code
4593/// bb1:
4594/// %0 = icmp ne i32 %a, 0
4595/// br i1 %0, label %TrueBB, label %bb2
4596/// bb2:
4597/// %1 = icmp ne i32 %b, 0
4598/// br i1 %1, label %TrueBB, label %FalseBB
4599/// \endcode
4600/// This usually allows instruction selection to do even further optimizations
4601/// and combine the compare with the branch instruction. Currently this is
4602/// applied for targets which have "cheap" jump instructions.
4603///
4604/// FIXME: Remove the (equivalent?) implementation in SelectionDAG.
4605///
4606bool CodeGenPrepare::splitBranchCondition(Function &F) {
David Blaikiedc3f01e2015-03-09 01:57:13 +00004607 if (!TM || !TM->Options.EnableFastISel || !TLI || TLI->isJumpExpensive())
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004608 return false;
4609
4610 bool MadeChange = false;
4611 for (auto &BB : F) {
4612 // Does this BB end with the following?
4613 // %cond1 = icmp|fcmp|binary instruction ...
4614 // %cond2 = icmp|fcmp|binary instruction ...
4615 // %cond.or = or|and i1 %cond1, cond2
4616 // br i1 %cond.or label %dest1, label %dest2"
4617 BinaryOperator *LogicOp;
4618 BasicBlock *TBB, *FBB;
4619 if (!match(BB.getTerminator(), m_Br(m_OneUse(m_BinOp(LogicOp)), TBB, FBB)))
4620 continue;
4621
4622 unsigned Opc;
Juergen Ributzka8bda7382014-12-09 17:50:10 +00004623 Value *Cond1, *Cond2;
4624 if (match(LogicOp, m_And(m_OneUse(m_Value(Cond1)),
4625 m_OneUse(m_Value(Cond2)))))
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004626 Opc = Instruction::And;
Juergen Ributzka8bda7382014-12-09 17:50:10 +00004627 else if (match(LogicOp, m_Or(m_OneUse(m_Value(Cond1)),
4628 m_OneUse(m_Value(Cond2)))))
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004629 Opc = Instruction::Or;
4630 else
4631 continue;
4632
4633 if (!match(Cond1, m_CombineOr(m_Cmp(), m_BinOp())) ||
4634 !match(Cond2, m_CombineOr(m_Cmp(), m_BinOp())) )
4635 continue;
4636
4637 DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump());
4638
4639 // Create a new BB.
4640 auto *InsertBefore = std::next(Function::iterator(BB))
4641 .getNodePtrUnchecked();
4642 auto TmpBB = BasicBlock::Create(BB.getContext(),
4643 BB.getName() + ".cond.split",
4644 BB.getParent(), InsertBefore);
4645
4646 // Update original basic block by using the first condition directly by the
4647 // branch instruction and removing the no longer needed and/or instruction.
4648 auto *Br1 = cast<BranchInst>(BB.getTerminator());
4649 Br1->setCondition(Cond1);
4650 LogicOp->eraseFromParent();
Juergen Ributzka8bda7382014-12-09 17:50:10 +00004651
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004652 // Depending on the conditon we have to either replace the true or the false
4653 // successor of the original branch instruction.
4654 if (Opc == Instruction::And)
4655 Br1->setSuccessor(0, TmpBB);
4656 else
4657 Br1->setSuccessor(1, TmpBB);
4658
4659 // Fill in the new basic block.
4660 auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB);
Juergen Ributzka8bda7382014-12-09 17:50:10 +00004661 if (auto *I = dyn_cast<Instruction>(Cond2)) {
4662 I->removeFromParent();
4663 I->insertBefore(Br2);
4664 }
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004665
4666 // Update PHI nodes in both successors. The original BB needs to be
4667 // replaced in one succesor's PHI nodes, because the branch comes now from
4668 // the newly generated BB (NewBB). In the other successor we need to add one
4669 // incoming edge to the PHI nodes, because both branch instructions target
4670 // now the same successor. Depending on the original branch condition
4671 // (and/or) we have to swap the successors (TrueDest, FalseDest), so that
4672 // we perfrom the correct update for the PHI nodes.
4673 // This doesn't change the successor order of the just created branch
4674 // instruction (or any other instruction).
4675 if (Opc == Instruction::Or)
4676 std::swap(TBB, FBB);
4677
4678 // Replace the old BB with the new BB.
4679 for (auto &I : *TBB) {
4680 PHINode *PN = dyn_cast<PHINode>(&I);
4681 if (!PN)
4682 break;
4683 int i;
4684 while ((i = PN->getBasicBlockIndex(&BB)) >= 0)
4685 PN->setIncomingBlock(i, TmpBB);
4686 }
4687
4688 // Add another incoming edge form the new BB.
4689 for (auto &I : *FBB) {
4690 PHINode *PN = dyn_cast<PHINode>(&I);
4691 if (!PN)
4692 break;
4693 auto *Val = PN->getIncomingValueForBlock(&BB);
4694 PN->addIncoming(Val, TmpBB);
4695 }
4696
4697 // Update the branch weights (from SelectionDAGBuilder::
4698 // FindMergedConditions).
4699 if (Opc == Instruction::Or) {
4700 // Codegen X | Y as:
4701 // BB1:
4702 // jmp_if_X TBB
4703 // jmp TmpBB
4704 // TmpBB:
4705 // jmp_if_Y TBB
4706 // jmp FBB
4707 //
4708
4709 // We have flexibility in setting Prob for BB1 and Prob for NewBB.
4710 // The requirement is that
4711 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
4712 // = TrueProb for orignal BB.
4713 // Assuming the orignal weights are A and B, one choice is to set BB1's
4714 // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice
4715 // assumes that
4716 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
4717 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
4718 // TmpBB, but the math is more complicated.
4719 uint64_t TrueWeight, FalseWeight;
Juergen Ributzka194350a2014-12-09 17:32:12 +00004720 if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) {
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004721 uint64_t NewTrueWeight = TrueWeight;
4722 uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
4723 scaleWeights(NewTrueWeight, NewFalseWeight);
4724 Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
4725 .createBranchWeights(TrueWeight, FalseWeight));
4726
4727 NewTrueWeight = TrueWeight;
4728 NewFalseWeight = 2 * FalseWeight;
4729 scaleWeights(NewTrueWeight, NewFalseWeight);
4730 Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
4731 .createBranchWeights(TrueWeight, FalseWeight));
4732 }
4733 } else {
4734 // Codegen X & Y as:
4735 // BB1:
4736 // jmp_if_X TmpBB
4737 // jmp FBB
4738 // TmpBB:
4739 // jmp_if_Y TBB
4740 // jmp FBB
4741 //
4742 // This requires creation of TmpBB after CurBB.
4743
4744 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
4745 // The requirement is that
4746 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
4747 // = FalseProb for orignal BB.
4748 // Assuming the orignal weights are A and B, one choice is to set BB1's
4749 // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice
4750 // assumes that
4751 // FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB.
4752 uint64_t TrueWeight, FalseWeight;
Juergen Ributzka194350a2014-12-09 17:32:12 +00004753 if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) {
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004754 uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
4755 uint64_t NewFalseWeight = FalseWeight;
4756 scaleWeights(NewTrueWeight, NewFalseWeight);
4757 Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
4758 .createBranchWeights(TrueWeight, FalseWeight));
4759
4760 NewTrueWeight = 2 * TrueWeight;
4761 NewFalseWeight = FalseWeight;
4762 scaleWeights(NewTrueWeight, NewFalseWeight);
4763 Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
4764 .createBranchWeights(TrueWeight, FalseWeight));
4765 }
4766 }
4767
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004768 // Note: No point in getting fancy here, since the DT info is never
Quentin Colombet7bdd50d2015-03-18 23:17:28 +00004769 // available to CodeGenPrepare.
Juergen Ributzkac1bbcbb2014-12-09 16:36:13 +00004770 ModifiedDT = true;
4771
4772 MadeChange = true;
4773
4774 DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump();
4775 TmpBB->dump());
4776 }
4777 return MadeChange;
4778}