blob: aae1cce80645eb54fd6aa62fa2f3da4237a627c4 [file] [log] [blame]
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +00001//===- NaryReassociate.cpp - Reassociate n-ary expressions ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass reassociates n-ary add expressions and eliminates the redundancy
11// exposed by the reassociation.
12//
13// A motivating example:
14//
15// void foo(int a, int b) {
16// bar(a + b);
17// bar((a + 2) + b);
18// }
19//
20// An ideal compiler should reassociate (a + 2) + b to (a + b) + 2 and simplify
21// the above code to
22//
23// int t = a + b;
24// bar(t);
25// bar(t + 2);
26//
27// However, the Reassociate pass is unable to do that because it processes each
28// instruction individually and believes (a + 2) + b is the best form according
29// to its rank system.
30//
31// To address this limitation, NaryReassociate reassociates an expression in a
32// form that reuses existing instructions. As a result, NaryReassociate can
33// reassociate (a + 2) + b in the example to (a + b) + 2 because it detects that
34// (a + b) is computed before.
35//
36// NaryReassociate works as follows. For every instruction in the form of (a +
37// b) + c, it checks whether a + c or b + c is already computed by a dominating
38// instruction. If so, it then reassociates (a + b) + c into (a + c) + b or (b +
Jingyue Wu8579b812015-04-17 00:25:10 +000039// c) + a and removes the redundancy accordingly. To efficiently look up whether
40// an expression is computed before, we store each instruction seen and its SCEV
41// into an SCEV-to-instruction map.
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000042//
43// Although the algorithm pattern-matches only ternary additions, it
44// automatically handles many >3-ary expressions by walking through the function
45// in the depth-first order. For example, given
46//
47// (a + c) + d
48// ((a + b) + c) + d
49//
50// NaryReassociate first rewrites (a + b) + c to (a + c) + b, and then rewrites
51// ((a + c) + b) + d into ((a + c) + d) + b.
52//
Jingyue Wu8579b812015-04-17 00:25:10 +000053// Finally, the above dominator-based algorithm may need to be run multiple
54// iterations before emitting optimal code. One source of this need is that we
55// only split an operand when it is used only once. The above algorithm can
56// eliminate an instruction and decrease the usage count of its operands. As a
57// result, an instruction that previously had multiple uses may become a
58// single-use instruction and thus eligible for split consideration. For
59// example,
60//
61// ac = a + c
62// ab = a + b
63// abc = ab + c
64// ab2 = ab + b
65// ab2c = ab2 + c
66//
67// In the first iteration, we cannot reassociate abc to ac+b because ab is used
68// twice. However, we can reassociate ab2c to abc+b in the first iteration. As a
69// result, ab2 becomes dead and ab will be used only once in the second
70// iteration.
71//
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000072// Limitations and TODO items:
73//
74// 1) We only considers n-ary adds for now. This should be extended and
75// generalized.
76//
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000077//===----------------------------------------------------------------------===//
78
Jingyue Wucf02ef32015-07-01 03:38:49 +000079#include "llvm/Analysis/AssumptionCache.h"
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000080#include "llvm/Analysis/ScalarEvolution.h"
Jingyue Wu8579b812015-04-17 00:25:10 +000081#include "llvm/Analysis/TargetLibraryInfo.h"
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +000082#include "llvm/Analysis/TargetTransformInfo.h"
Jingyue Wucf02ef32015-07-01 03:38:49 +000083#include "llvm/Analysis/ValueTracking.h"
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000084#include "llvm/IR/Dominators.h"
85#include "llvm/IR/Module.h"
86#include "llvm/IR/PatternMatch.h"
Jingyue Wucf02ef32015-07-01 03:38:49 +000087#include "llvm/Support/Debug.h"
88#include "llvm/Support/raw_ostream.h"
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000089#include "llvm/Transforms/Scalar.h"
Jingyue Wu8579b812015-04-17 00:25:10 +000090#include "llvm/Transforms/Utils/Local.h"
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +000091using namespace llvm;
92using namespace PatternMatch;
93
94#define DEBUG_TYPE "nary-reassociate"
95
96namespace {
97class NaryReassociate : public FunctionPass {
98public:
99 static char ID;
100
101 NaryReassociate(): FunctionPass(ID) {
102 initializeNaryReassociatePass(*PassRegistry::getPassRegistry());
103 }
104
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000105 bool doInitialization(Module &M) override {
106 DL = &M.getDataLayout();
107 return false;
108 }
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000109 bool runOnFunction(Function &F) override;
110
111 void getAnalysisUsage(AnalysisUsage &AU) const override {
112 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000113 AU.addPreserved<ScalarEvolutionWrapperPass>();
Jingyue Wu8579b812015-04-17 00:25:10 +0000114 AU.addPreserved<TargetLibraryInfoWrapperPass>();
Jingyue Wucf02ef32015-07-01 03:38:49 +0000115 AU.addRequired<AssumptionCacheTracker>();
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000116 AU.addRequired<DominatorTreeWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000117 AU.addRequired<ScalarEvolutionWrapperPass>();
Jingyue Wu8579b812015-04-17 00:25:10 +0000118 AU.addRequired<TargetLibraryInfoWrapperPass>();
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000119 AU.addRequired<TargetTransformInfoWrapperPass>();
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000120 AU.setPreservesCFG();
121 }
122
123private:
Jingyue Wu8579b812015-04-17 00:25:10 +0000124 // Runs only one iteration of the dominator-based algorithm. See the header
125 // comments for why we need multiple iterations.
126 bool doOneIteration(Function &F);
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000127
128 // Reassociates I for better CSE.
129 Instruction *tryReassociate(Instruction *I);
130
131 // Reassociate GEP for better CSE.
132 Instruction *tryReassociateGEP(GetElementPtrInst *GEP);
133 // Try splitting GEP at the I-th index and see whether either part can be
134 // CSE'ed. This is a helper function for tryReassociateGEP.
135 //
136 // \p IndexedType The element type indexed by GEP's I-th index. This is
137 // equivalent to
138 // GEP->getIndexedType(GEP->getPointerOperand(), 0-th index,
139 // ..., i-th index).
140 GetElementPtrInst *tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
141 unsigned I, Type *IndexedType);
142 // Given GEP's I-th index = LHS + RHS, see whether &Base[..][LHS][..] or
143 // &Base[..][RHS][..] can be CSE'ed and rewrite GEP accordingly.
144 GetElementPtrInst *tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
145 unsigned I, Value *LHS,
146 Value *RHS, Type *IndexedType);
147
148 // Reassociate Add for better CSE.
149 Instruction *tryReassociateAdd(BinaryOperator *I);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000150 // A helper function for tryReassociateAdd. LHS and RHS are explicitly passed.
151 Instruction *tryReassociateAdd(Value *LHS, Value *RHS, Instruction *I);
152 // Rewrites I to LHS + RHS if LHS is computed already.
153 Instruction *tryReassociatedAdd(const SCEV *LHS, Value *RHS, Instruction *I);
154
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000155 // Returns the closest dominator of \c Dominatee that computes
156 // \c CandidateExpr. Returns null if not found.
157 Instruction *findClosestMatchingDominator(const SCEV *CandidateExpr,
158 Instruction *Dominatee);
159 // GetElementPtrInst implicitly sign-extends an index if the index is shorter
160 // than the pointer size. This function returns whether Index is shorter than
161 // GEP's pointer size, i.e., whether Index needs to be sign-extended in order
162 // to be an index of GEP.
163 bool requiresSignExtension(Value *Index, GetElementPtrInst *GEP);
Jingyue Wucf02ef32015-07-01 03:38:49 +0000164 // Returns whether V is known to be non-negative at context \c Ctxt.
165 bool isKnownNonNegative(Value *V, Instruction *Ctxt);
166 // Returns whether AO may sign overflow at context \c Ctxt. It computes a
167 // conservative result -- it answers true when not sure.
168 bool maySignOverflow(AddOperator *AO, Instruction *Ctxt);
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000169
Jingyue Wucf02ef32015-07-01 03:38:49 +0000170 AssumptionCache *AC;
171 const DataLayout *DL;
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000172 DominatorTree *DT;
173 ScalarEvolution *SE;
Jingyue Wu8579b812015-04-17 00:25:10 +0000174 TargetLibraryInfo *TLI;
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000175 TargetTransformInfo *TTI;
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000176 // A lookup table quickly telling which instructions compute the given SCEV.
177 // Note that there can be multiple instructions at different locations
Jingyue Wu771dfe92015-04-16 18:42:31 +0000178 // computing to the same SCEV, so we map a SCEV to an instruction list. For
179 // example,
180 //
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000181 // if (p1)
182 // foo(a + b);
183 // if (p2)
184 // bar(a + b);
185 DenseMap<const SCEV *, SmallVector<Instruction *, 2>> SeenExprs;
186};
187} // anonymous namespace
188
189char NaryReassociate::ID = 0;
190INITIALIZE_PASS_BEGIN(NaryReassociate, "nary-reassociate", "Nary reassociation",
191 false, false)
Jingyue Wucf02ef32015-07-01 03:38:49 +0000192INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000193INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000194INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Jingyue Wu8579b812015-04-17 00:25:10 +0000195INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000196INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000197INITIALIZE_PASS_END(NaryReassociate, "nary-reassociate", "Nary reassociation",
198 false, false)
199
200FunctionPass *llvm::createNaryReassociatePass() {
201 return new NaryReassociate();
202}
203
204bool NaryReassociate::runOnFunction(Function &F) {
205 if (skipOptnoneFunction(F))
206 return false;
207
Jingyue Wucf02ef32015-07-01 03:38:49 +0000208 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000209 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000210 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Jingyue Wu8579b812015-04-17 00:25:10 +0000211 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000212 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000213
Jingyue Wu8579b812015-04-17 00:25:10 +0000214 bool Changed = false, ChangedInThisIteration;
215 do {
216 ChangedInThisIteration = doOneIteration(F);
217 Changed |= ChangedInThisIteration;
218 } while (ChangedInThisIteration);
219 return Changed;
220}
221
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000222// Whitelist the instruction types NaryReassociate handles for now.
223static bool isPotentiallyNaryReassociable(Instruction *I) {
224 switch (I->getOpcode()) {
225 case Instruction::Add:
226 case Instruction::GetElementPtr:
227 return true;
228 default:
229 return false;
230 }
231}
232
Jingyue Wu8579b812015-04-17 00:25:10 +0000233bool NaryReassociate::doOneIteration(Function &F) {
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000234 bool Changed = false;
235 SeenExprs.clear();
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000236 // Process the basic blocks in pre-order of the dominator tree. This order
237 // ensures that all bases of a candidate are in Candidates when we process it.
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000238 for (auto Node = GraphTraits<DominatorTree *>::nodes_begin(DT);
239 Node != GraphTraits<DominatorTree *>::nodes_end(DT); ++Node) {
240 BasicBlock *BB = Node->getBlock();
241 for (auto I = BB->begin(); I != BB->end(); ++I) {
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000242 if (SE->isSCEVable(I->getType()) && isPotentiallyNaryReassociable(I)) {
Jingyue Wuc2a01462015-05-28 04:56:52 +0000243 const SCEV *OldSCEV = SE->getSCEV(I);
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000244 if (Instruction *NewI = tryReassociate(I)) {
Jingyue Wu8579b812015-04-17 00:25:10 +0000245 Changed = true;
246 SE->forgetValue(I);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000247 I->replaceAllUsesWith(NewI);
Jingyue Wu8579b812015-04-17 00:25:10 +0000248 RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000249 I = NewI;
250 }
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000251 // Add the rewritten instruction to SeenExprs; the original instruction
252 // is deleted.
Jingyue Wuc2a01462015-05-28 04:56:52 +0000253 const SCEV *NewSCEV = SE->getSCEV(I);
254 SeenExprs[NewSCEV].push_back(I);
255 // Ideally, NewSCEV should equal OldSCEV because tryReassociate(I)
256 // is equivalent to I. However, ScalarEvolution::getSCEV may
257 // weaken nsw causing NewSCEV not to equal OldSCEV. For example, suppose
258 // we reassociate
259 // I = &a[sext(i +nsw j)] // assuming sizeof(a[0]) = 4
260 // to
261 // NewI = &a[sext(i)] + sext(j).
262 //
263 // ScalarEvolution computes
264 // getSCEV(I) = a + 4 * sext(i + j)
265 // getSCEV(newI) = a + 4 * sext(i) + 4 * sext(j)
266 // which are different SCEVs.
267 //
268 // To alleviate this issue of ScalarEvolution not always capturing
269 // equivalence, we add I to SeenExprs[OldSCEV] as well so that we can
270 // map both SCEV before and after tryReassociate(I) to I.
271 //
272 // This improvement is exercised in @reassociate_gep_nsw in nary-gep.ll.
273 if (NewSCEV != OldSCEV)
274 SeenExprs[OldSCEV].push_back(I);
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000275 }
276 }
277 }
278 return Changed;
279}
280
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000281Instruction *NaryReassociate::tryReassociate(Instruction *I) {
282 switch (I->getOpcode()) {
283 case Instruction::Add:
284 return tryReassociateAdd(cast<BinaryOperator>(I));
285 case Instruction::GetElementPtr:
286 return tryReassociateGEP(cast<GetElementPtrInst>(I));
287 default:
288 llvm_unreachable("should be filtered out by isPotentiallyNaryReassociable");
289 }
290}
291
292// FIXME: extract this method into TTI->getGEPCost.
293static bool isGEPFoldable(GetElementPtrInst *GEP,
294 const TargetTransformInfo *TTI,
295 const DataLayout *DL) {
296 GlobalVariable *BaseGV = nullptr;
297 int64_t BaseOffset = 0;
298 bool HasBaseReg = false;
299 int64_t Scale = 0;
300
301 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getPointerOperand()))
302 BaseGV = GV;
303 else
304 HasBaseReg = true;
305
306 gep_type_iterator GTI = gep_type_begin(GEP);
307 for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I, ++GTI) {
308 if (isa<SequentialType>(*GTI)) {
309 int64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
310 if (ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I)) {
311 BaseOffset += ConstIdx->getSExtValue() * ElementSize;
312 } else {
313 // Needs scale register.
314 if (Scale != 0) {
315 // No addressing mode takes two scale registers.
316 return false;
317 }
318 Scale = ElementSize;
319 }
320 } else {
321 StructType *STy = cast<StructType>(*GTI);
322 uint64_t Field = cast<ConstantInt>(*I)->getZExtValue();
323 BaseOffset += DL->getStructLayout(STy)->getElementOffset(Field);
324 }
325 }
Matt Arsenaultfb88aca2015-06-07 20:17:42 +0000326
327 unsigned AddrSpace = GEP->getPointerAddressSpace();
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000328 return TTI->isLegalAddressingMode(GEP->getType()->getElementType(), BaseGV,
Matt Arsenaultfb88aca2015-06-07 20:17:42 +0000329 BaseOffset, HasBaseReg, Scale, AddrSpace);
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000330}
331
332Instruction *NaryReassociate::tryReassociateGEP(GetElementPtrInst *GEP) {
333 // Not worth reassociating GEP if it is foldable.
334 if (isGEPFoldable(GEP, TTI, DL))
335 return nullptr;
336
337 gep_type_iterator GTI = gep_type_begin(*GEP);
338 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I) {
339 if (isa<SequentialType>(*GTI++)) {
340 if (auto *NewGEP = tryReassociateGEPAtIndex(GEP, I - 1, *GTI)) {
341 return NewGEP;
342 }
343 }
344 }
345 return nullptr;
346}
347
348bool NaryReassociate::requiresSignExtension(Value *Index,
349 GetElementPtrInst *GEP) {
350 unsigned PointerSizeInBits =
351 DL->getPointerSizeInBits(GEP->getType()->getPointerAddressSpace());
352 return cast<IntegerType>(Index->getType())->getBitWidth() < PointerSizeInBits;
353}
354
Jingyue Wucf02ef32015-07-01 03:38:49 +0000355bool NaryReassociate::isKnownNonNegative(Value *V, Instruction *Ctxt) {
356 bool NonNegative, Negative;
357 // TODO: ComputeSignBits is expensive. Consider caching the results.
358 ComputeSignBit(V, NonNegative, Negative, *DL, 0, AC, Ctxt, DT);
359 return NonNegative;
360}
361
362bool NaryReassociate::maySignOverflow(AddOperator *AO, Instruction *Ctxt) {
363 if (AO->hasNoSignedWrap())
364 return false;
365
366 Value *LHS = AO->getOperand(0), *RHS = AO->getOperand(1);
367 // If LHS or RHS has the same sign as the sum, AO doesn't sign overflow.
368 // TODO: handle the negative case as well.
369 if (isKnownNonNegative(AO, Ctxt) &&
370 (isKnownNonNegative(LHS, Ctxt) || isKnownNonNegative(RHS, Ctxt)))
371 return false;
372
373 return true;
374}
375
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000376GetElementPtrInst *
377NaryReassociate::tryReassociateGEPAtIndex(GetElementPtrInst *GEP, unsigned I,
378 Type *IndexedType) {
379 Value *IndexToSplit = GEP->getOperand(I + 1);
Jingyue Wucf02ef32015-07-01 03:38:49 +0000380 if (SExtInst *SExt = dyn_cast<SExtInst>(IndexToSplit)) {
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000381 IndexToSplit = SExt->getOperand(0);
Jingyue Wucf02ef32015-07-01 03:38:49 +0000382 } else if (ZExtInst *ZExt = dyn_cast<ZExtInst>(IndexToSplit)) {
383 // zext can be treated as sext if the source is non-negative.
384 if (isKnownNonNegative(ZExt->getOperand(0), GEP))
385 IndexToSplit = ZExt->getOperand(0);
386 }
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000387
388 if (AddOperator *AO = dyn_cast<AddOperator>(IndexToSplit)) {
389 // If the I-th index needs sext and the underlying add is not equipped with
390 // nsw, we cannot split the add because
391 // sext(LHS + RHS) != sext(LHS) + sext(RHS).
Jingyue Wucf02ef32015-07-01 03:38:49 +0000392 if (requiresSignExtension(IndexToSplit, GEP) && maySignOverflow(AO, GEP))
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000393 return nullptr;
394 Value *LHS = AO->getOperand(0), *RHS = AO->getOperand(1);
395 // IndexToSplit = LHS + RHS.
396 if (auto *NewGEP = tryReassociateGEPAtIndex(GEP, I, LHS, RHS, IndexedType))
397 return NewGEP;
398 // Symmetrically, try IndexToSplit = RHS + LHS.
399 if (LHS != RHS) {
400 if (auto *NewGEP =
401 tryReassociateGEPAtIndex(GEP, I, RHS, LHS, IndexedType))
402 return NewGEP;
403 }
404 }
405 return nullptr;
406}
407
Jingyue Wucf02ef32015-07-01 03:38:49 +0000408GetElementPtrInst *NaryReassociate::tryReassociateGEPAtIndex(
409 GetElementPtrInst *GEP, unsigned I, Value *LHS, Value *RHS,
410 Type *IndexedType) {
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000411 // Look for GEP's closest dominator that has the same SCEV as GEP except that
412 // the I-th index is replaced with LHS.
413 SmallVector<const SCEV *, 4> IndexExprs;
414 for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
415 IndexExprs.push_back(SE->getSCEV(*Index));
416 // Replace the I-th index with LHS.
417 IndexExprs[I] = SE->getSCEV(LHS);
Jingyue Wucf02ef32015-07-01 03:38:49 +0000418 if (isKnownNonNegative(LHS, GEP) &&
419 DL->getTypeSizeInBits(LHS->getType()) <
420 DL->getTypeSizeInBits(GEP->getOperand(I)->getType())) {
421 // Zero-extend LHS if it is non-negative. InstCombine canonicalizes sext to
422 // zext if the source operand is proved non-negative. We should do that
423 // consistently so that CandidateExpr more likely appears before. See
424 // @reassociate_gep_assume for an example of this canonicalization.
425 IndexExprs[I] =
426 SE->getZeroExtendExpr(IndexExprs[I], GEP->getOperand(I)->getType());
427 }
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000428 const SCEV *CandidateExpr = SE->getGEPExpr(
429 GEP->getSourceElementType(), SE->getSCEV(GEP->getPointerOperand()),
430 IndexExprs, GEP->isInBounds());
431
432 auto *Candidate = findClosestMatchingDominator(CandidateExpr, GEP);
433 if (Candidate == nullptr)
434 return nullptr;
435
436 PointerType *TypeOfCandidate = dyn_cast<PointerType>(Candidate->getType());
437 // Pretty rare but theoretically possible when a numeric value happens to
438 // share CandidateExpr.
439 if (TypeOfCandidate == nullptr)
440 return nullptr;
441
442 // NewGEP = (char *)Candidate + RHS * sizeof(IndexedType)
443 uint64_t IndexedSize = DL->getTypeAllocSize(IndexedType);
444 Type *ElementType = TypeOfCandidate->getElementType();
445 uint64_t ElementSize = DL->getTypeAllocSize(ElementType);
446 // Another less rare case: because I is not necessarily the last index of the
447 // GEP, the size of the type at the I-th index (IndexedSize) is not
448 // necessarily divisible by ElementSize. For example,
449 //
450 // #pragma pack(1)
451 // struct S {
452 // int a[3];
453 // int64 b[8];
454 // };
455 // #pragma pack()
456 //
457 // sizeof(S) = 100 is indivisible by sizeof(int64) = 8.
458 //
459 // TODO: bail out on this case for now. We could emit uglygep.
460 if (IndexedSize % ElementSize != 0)
461 return nullptr;
462
463 // NewGEP = &Candidate[RHS * (sizeof(IndexedType) / sizeof(Candidate[0])));
464 IRBuilder<> Builder(GEP);
465 Type *IntPtrTy = DL->getIntPtrType(TypeOfCandidate);
466 if (RHS->getType() != IntPtrTy)
467 RHS = Builder.CreateSExtOrTrunc(RHS, IntPtrTy);
468 if (IndexedSize != ElementSize) {
469 RHS = Builder.CreateMul(
470 RHS, ConstantInt::get(IntPtrTy, IndexedSize / ElementSize));
471 }
472 GetElementPtrInst *NewGEP =
473 cast<GetElementPtrInst>(Builder.CreateGEP(Candidate, RHS));
474 NewGEP->setIsInBounds(GEP->isInBounds());
475 NewGEP->takeName(GEP);
476 return NewGEP;
477}
478
479Instruction *NaryReassociate::tryReassociateAdd(BinaryOperator *I) {
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000480 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
481 if (auto *NewI = tryReassociateAdd(LHS, RHS, I))
482 return NewI;
483 if (auto *NewI = tryReassociateAdd(RHS, LHS, I))
484 return NewI;
485 return nullptr;
486}
487
488Instruction *NaryReassociate::tryReassociateAdd(Value *LHS, Value *RHS,
489 Instruction *I) {
490 Value *A = nullptr, *B = nullptr;
491 // To be conservative, we reassociate I only when it is the only user of A+B.
492 if (LHS->hasOneUse() && match(LHS, m_Add(m_Value(A), m_Value(B)))) {
493 // I = (A + B) + RHS
494 // = (A + RHS) + B or (B + RHS) + A
495 const SCEV *AExpr = SE->getSCEV(A), *BExpr = SE->getSCEV(B);
496 const SCEV *RHSExpr = SE->getSCEV(RHS);
Jingyue Wuc74e33b2015-05-13 18:12:24 +0000497 if (BExpr != RHSExpr) {
498 if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(AExpr, RHSExpr), B, I))
499 return NewI;
500 }
501 if (AExpr != RHSExpr) {
502 if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(BExpr, RHSExpr), A, I))
503 return NewI;
504 }
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000505 }
506 return nullptr;
507}
508
509Instruction *NaryReassociate::tryReassociatedAdd(const SCEV *LHSExpr,
510 Value *RHS, Instruction *I) {
Jingyue Wu771dfe92015-04-16 18:42:31 +0000511 // Look for the closest dominator LHS of I that computes LHSExpr, and replace
512 // I with LHS + RHS.
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000513 auto *LHS = findClosestMatchingDominator(LHSExpr, I);
514 if (LHS == nullptr)
515 return nullptr;
516
517 Instruction *NewI = BinaryOperator::CreateAdd(LHS, RHS, "", I);
518 NewI->takeName(I);
519 return NewI;
520}
521
522Instruction *
523NaryReassociate::findClosestMatchingDominator(const SCEV *CandidateExpr,
524 Instruction *Dominatee) {
525 auto Pos = SeenExprs.find(CandidateExpr);
526 if (Pos == SeenExprs.end())
527 return nullptr;
528
529 auto &Candidates = Pos->second;
530 // Because we process the basic blocks in pre-order of the dominator tree, a
Jingyue Wu771dfe92015-04-16 18:42:31 +0000531 // candidate that doesn't dominate the current instruction won't dominate any
532 // future instruction either. Therefore, we pop it out of the stack. This
533 // optimization makes the algorithm O(n).
Jingyue Wu4fc97f6d2015-05-21 23:17:30 +0000534 while (!Candidates.empty()) {
535 Instruction *Candidate = Candidates.back();
536 if (DT->dominates(Candidate, Dominatee))
537 return Candidate;
538 Candidates.pop_back();
Jingyue Wu8cb6b2a2015-04-14 04:59:22 +0000539 }
540 return nullptr;
541}