blob: 9acfed661d8238e2e9c5e19959b05da6caa8d354 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===- ScopHelper.cpp - Some Helper Functions for Scop. ------------------===//
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// Small functions that help with Scop and LLVM-IR.
11//
12//===----------------------------------------------------------------------===//
13
14#include "polly/Support/ScopHelper.h"
Tobias Grosser8edce4e2013-04-16 08:04:42 +000015#include "polly/ScopInfo.h"
Chandler Carruth5ec33332015-01-18 10:52:23 +000016#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser75805372011-04-29 06:27:02 +000017#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/Analysis/RegionInfo.h"
19#include "llvm/Analysis/ScalarEvolution.h"
Johannes Doerferte69e1142015-08-18 11:56:00 +000020#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser75805372011-04-29 06:27:02 +000021#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthc3478b92014-03-04 11:47:37 +000022#include "llvm/IR/CFG.h"
Chandler Carruth95fef942014-04-22 03:30:19 +000023#include "llvm/Support/Debug.h"
Tobias Grosser75805372011-04-29 06:27:02 +000024#include "llvm/Transforms/Utils/BasicBlockUtils.h"
25
Tobias Grosser75805372011-04-29 06:27:02 +000026using namespace llvm;
Johannes Doerferte69e1142015-08-18 11:56:00 +000027using namespace polly;
Tobias Grosser75805372011-04-29 06:27:02 +000028
Chandler Carruth95fef942014-04-22 03:30:19 +000029#define DEBUG_TYPE "polly-scop-helper"
30
Tobias Grosser75805372011-04-29 06:27:02 +000031Value *polly::getPointerOperand(Instruction &Inst) {
32 if (LoadInst *load = dyn_cast<LoadInst>(&Inst))
33 return load->getPointerOperand();
34 else if (StoreInst *store = dyn_cast<StoreInst>(&Inst))
35 return store->getPointerOperand();
36 else if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(&Inst))
37 return gep->getPointerOperand();
38
39 return 0;
40}
41
Tobias Grosser75805372011-04-29 06:27:02 +000042bool polly::hasInvokeEdge(const PHINode *PN) {
43 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
44 if (InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)))
45 if (II->getParent() == PN->getIncomingBlock(i))
46 return true;
47
48 return false;
49}
50
Michael Kruse22370882015-08-11 14:39:21 +000051// Ensures that there is just one predecessor to the entry node from outside the
52// region.
53// The identity of the region entry node is preserved.
54static void simplifyRegionEntry(Region *R, DominatorTree *DT, LoopInfo *LI,
55 RegionInfo *RI) {
Johannes Doerfert38262242014-09-10 14:50:23 +000056 BasicBlock *EnteringBB = R->getEnteringBlock();
Michael Kruse22370882015-08-11 14:39:21 +000057 BasicBlock *Entry = R->getEntry();
Johannes Doerfert38262242014-09-10 14:50:23 +000058
Michael Kruse22370882015-08-11 14:39:21 +000059 // Before (one of):
60 //
61 // \ / //
62 // EnteringBB //
63 // | \------> //
64 // \ / | //
65 // Entry <--\ Entry <--\ //
66 // / \ / / \ / //
67 // .... .... //
Chandler Carruth62975f52015-01-19 12:37:33 +000068
Tobias Grosser8edce4e2013-04-16 08:04:42 +000069 // Create single entry edge if the region has multiple entry edges.
Johannes Doerfert38262242014-09-10 14:50:23 +000070 if (!EnteringBB) {
Michael Kruse22370882015-08-11 14:39:21 +000071 SmallVector<BasicBlock *, 4> Preds;
72 for (BasicBlock *P : predecessors(Entry))
73 if (!R->contains(P))
74 Preds.push_back(P);
Johannes Doerfert38262242014-09-10 14:50:23 +000075
Michael Kruse22370882015-08-11 14:39:21 +000076 BasicBlock *NewEntering =
77 SplitBlockPredecessors(Entry, Preds, ".region_entering", DT, LI);
Johannes Doerfert0fe35dd2014-09-15 18:34:45 +000078
Michael Kruse22370882015-08-11 14:39:21 +000079 if (RI) {
80 // The exit block of predecessing regions must be changed to NewEntering
81 for (BasicBlock *ExitPred : predecessors(NewEntering)) {
82 Region *RegionOfPred = RI->getRegionFor(ExitPred);
83 if (RegionOfPred->getExit() != Entry)
84 continue;
Johannes Doerfert0fe35dd2014-09-15 18:34:45 +000085
Michael Kruse22370882015-08-11 14:39:21 +000086 while (!RegionOfPred->isTopLevelRegion() &&
87 RegionOfPred->getExit() == Entry) {
88 RegionOfPred->replaceExit(NewEntering);
89 RegionOfPred = RegionOfPred->getParent();
90 }
91 }
92
93 // Make all ancestors use EnteringBB as entry; there might be edges to it
94 Region *AncestorR = R->getParent();
95 RI->setRegionFor(NewEntering, AncestorR);
96 while (!AncestorR->isTopLevelRegion() && AncestorR->getEntry() == Entry) {
97 AncestorR->replaceEntry(NewEntering);
98 AncestorR = AncestorR->getParent();
99 }
Johannes Doerfert0fe35dd2014-09-15 18:34:45 +0000100 }
101
Michael Kruse22370882015-08-11 14:39:21 +0000102 EnteringBB = NewEntering;
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000103 }
Michael Kruse22370882015-08-11 14:39:21 +0000104 assert(R->getEnteringBlock() == EnteringBB);
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000105
Michael Kruse22370882015-08-11 14:39:21 +0000106 // After:
107 //
108 // \ / //
109 // EnteringBB //
110 // | //
111 // | //
112 // Entry <--\ //
113 // / \ / //
114 // .... //
115}
Johannes Doerfert0fe35dd2014-09-15 18:34:45 +0000116
Michael Kruse22370882015-08-11 14:39:21 +0000117// Ensure that the region has a single block that branches to the exit node.
118static void simplifyRegionExit(Region *R, DominatorTree *DT, LoopInfo *LI,
119 RegionInfo *RI) {
120 BasicBlock *ExitBB = R->getExit();
121 BasicBlock *ExitingBB = R->getExitingBlock();
122
123 // Before:
124 //
125 // (Region) ______/ //
126 // \ | / //
127 // ExitBB //
128 // / \ //
129
130 if (!ExitingBB) {
131 SmallVector<BasicBlock *, 4> Preds;
132 for (BasicBlock *P : predecessors(ExitBB))
133 if (R->contains(P))
134 Preds.push_back(P);
135
136 // Preds[0] Preds[1] otherBB //
137 // \ | ________/ //
138 // \ | / //
139 // BB //
140 ExitingBB =
141 SplitBlockPredecessors(ExitBB, Preds, ".region_exiting", DT, LI);
142 // Preds[0] Preds[1] otherBB //
143 // \ / / //
144 // BB.region_exiting / //
145 // \ / //
146 // BB //
147
148 if (RI)
149 RI->setRegionFor(ExitingBB, R);
150
151 // Change the exit of nested regions, but not the region itself,
152 R->replaceExitRecursive(ExitingBB);
153 R->replaceExit(ExitBB);
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000154 }
Michael Kruse22370882015-08-11 14:39:21 +0000155 assert(ExitingBB == R->getExitingBlock());
Johannes Doerfert38262242014-09-10 14:50:23 +0000156
Michael Kruse22370882015-08-11 14:39:21 +0000157 // After:
158 //
159 // \ / //
160 // ExitingBB _____/ //
161 // \ / //
162 // ExitBB //
163 // / \ //
164}
165
166void polly::simplifyRegion(Region *R, DominatorTree *DT, LoopInfo *LI,
167 RegionInfo *RI) {
168 assert(R && !R->isTopLevelRegion());
169 assert(!RI || RI == R->getRegionInfo());
170 assert((!RI || DT) &&
171 "RegionInfo requires DominatorTree to be updated as well");
172
173 simplifyRegionEntry(R, DT, LI, RI);
174 simplifyRegionExit(R, DT, LI, RI);
175 assert(R->isSimple());
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000176}
177
Michael Kruse23d0e832015-08-11 14:04:06 +0000178// Split the block into two successive blocks.
179//
180// Like llvm::SplitBlock, but also preserves RegionInfo
181static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
182 DominatorTree *DT, llvm::LoopInfo *LI,
183 RegionInfo *RI) {
184 assert(Old && SplitPt);
185
186 // Before:
187 //
188 // \ / //
189 // Old //
190 // / \ //
191
192 BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
193
194 if (RI) {
195 Region *R = RI->getRegionFor(Old);
196 RI->setRegionFor(NewBlock, R);
197 }
198
199 // After:
200 //
201 // \ / //
202 // Old //
203 // | //
204 // NewBlock //
205 // / \ //
206
207 return NewBlock;
208}
209
Tobias Grosser75805372011-04-29 06:27:02 +0000210void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
211 // Find first non-alloca instruction. Every basic block has a non-alloc
212 // instruction, as every well formed basic block has a terminator.
213 BasicBlock::iterator I = EntryBlock->begin();
Tobias Grosserd535f552013-02-14 16:42:45 +0000214 while (isa<AllocaInst>(I))
215 ++I;
Tobias Grosser75805372011-04-29 06:27:02 +0000216
Chandler Carruth5ec33332015-01-18 10:52:23 +0000217 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
218 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
219 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
220 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Michael Kruse23d0e832015-08-11 14:04:06 +0000221 RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>();
222 RegionInfo *RI = RIP ? &RIP->getRegionInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000223
Michael Kruse23d0e832015-08-11 14:04:06 +0000224 // splitBlock updates DT, LI and RI.
225 splitBlock(EntryBlock, I, DT, LI, RI);
Tobias Grosser75805372011-04-29 06:27:02 +0000226}
Johannes Doerferte69e1142015-08-18 11:56:00 +0000227
228/// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
229/// instruction but just use it, if it is referenced as a SCEVUnknown. We want
230/// however to generate new code if the instruction is in the analyzed region
231/// and we generate code outside/in front of that region. Hence, we generate the
232/// code for the SDiv/SRem operands in front of the analyzed region and then
233/// create a new SDiv/SRem operation there too.
234struct ScopExpander : SCEVVisitor<ScopExpander, const SCEV *> {
235 friend struct SCEVVisitor<ScopExpander, const SCEV *>;
236
237 explicit ScopExpander(const Region &R, ScalarEvolution &SE,
Johannes Doerfertc0729a32015-09-30 16:52:03 +0000238 const DataLayout &DL, const char *Name, ValueMapT *VMap)
239 : Expander(SCEVExpander(SE, DL, Name)), SE(SE), Name(Name), R(R),
240 VMap(VMap) {}
Johannes Doerferte69e1142015-08-18 11:56:00 +0000241
242 Value *expandCodeFor(const SCEV *E, Type *Ty, Instruction *I) {
243 // If we generate code in the region we will immediately fall back to the
244 // SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
245 // needed replace them by copies computed in the entering block.
246 if (!R.contains(I))
247 E = visit(E);
248 return Expander.expandCodeFor(E, Ty, I);
249 }
250
251private:
252 SCEVExpander Expander;
253 ScalarEvolution &SE;
254 const char *Name;
255 const Region &R;
Johannes Doerfertc0729a32015-09-30 16:52:03 +0000256 ValueMapT *VMap;
Johannes Doerferte69e1142015-08-18 11:56:00 +0000257
258 const SCEV *visitUnknown(const SCEVUnknown *E) {
Johannes Doerfertc0729a32015-09-30 16:52:03 +0000259
260 // If a value mapping was given try if the underlying value is remapped.
261 if (VMap)
262 if (Value *NewVal = VMap->lookup(E->getValue()))
Johannes Doerfert59984322015-09-30 21:12:12 +0000263 if (NewVal != E->getValue())
264 return visit(SE.getSCEV(NewVal));
Johannes Doerfertc0729a32015-09-30 16:52:03 +0000265
Johannes Doerferte69e1142015-08-18 11:56:00 +0000266 Instruction *Inst = dyn_cast<Instruction>(E->getValue());
267 if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
268 Inst->getOpcode() != Instruction::SDiv))
269 return E;
270
271 if (!R.contains(Inst))
272 return E;
273
274 Instruction *StartIP = R.getEnteringBlock()->getTerminator();
275
276 const SCEV *LHSScev = visit(SE.getSCEV(Inst->getOperand(0)));
277 const SCEV *RHSScev = visit(SE.getSCEV(Inst->getOperand(1)));
278
279 Value *LHS = Expander.expandCodeFor(LHSScev, E->getType(), StartIP);
280 Value *RHS = Expander.expandCodeFor(RHSScev, E->getType(), StartIP);
281
282 Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
283 LHS, RHS, Inst->getName() + Name, StartIP);
284 return SE.getSCEV(Inst);
285 }
286
287 /// The following functions will just traverse the SCEV and rebuild it with
288 /// the new operands returned by the traversal.
289 ///
290 ///{
291 const SCEV *visitConstant(const SCEVConstant *E) { return E; }
292 const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
293 return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
294 }
295 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
296 return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
297 }
298 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
299 return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
300 }
301 const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
302 return SE.getUDivExpr(visit(E->getLHS()), visit(E->getRHS()));
303 }
304 const SCEV *visitAddExpr(const SCEVAddExpr *E) {
305 SmallVector<const SCEV *, 4> NewOps;
306 for (const SCEV *Op : E->operands())
307 NewOps.push_back(visit(Op));
308 return SE.getAddExpr(NewOps);
309 }
310 const SCEV *visitMulExpr(const SCEVMulExpr *E) {
311 SmallVector<const SCEV *, 4> NewOps;
312 for (const SCEV *Op : E->operands())
313 NewOps.push_back(visit(Op));
314 return SE.getMulExpr(NewOps);
315 }
316 const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
317 SmallVector<const SCEV *, 4> NewOps;
318 for (const SCEV *Op : E->operands())
319 NewOps.push_back(visit(Op));
320 return SE.getUMaxExpr(NewOps);
321 }
322 const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
323 SmallVector<const SCEV *, 4> NewOps;
324 for (const SCEV *Op : E->operands())
325 NewOps.push_back(visit(Op));
326 return SE.getSMaxExpr(NewOps);
327 }
328 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
329 SmallVector<const SCEV *, 4> NewOps;
330 for (const SCEV *Op : E->operands())
331 NewOps.push_back(visit(Op));
332 return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
333 }
334 ///}
335};
336
337Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
338 const char *Name, const SCEV *E, Type *Ty,
Johannes Doerfertc0729a32015-09-30 16:52:03 +0000339 Instruction *IP, ValueMapT *VMap) {
340 ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap);
Johannes Doerferte69e1142015-08-18 11:56:00 +0000341 return Expander.expandCodeFor(E, Ty, IP);
342}
Johannes Doerfert90db75e2015-09-10 17:51:27 +0000343
344bool polly::isErrorBlock(BasicBlock &BB) {
345
346 for (Instruction &Inst : BB)
347 if (CallInst *CI = dyn_cast<CallInst>(&Inst))
348 if (Function *F = CI->getCalledFunction())
349 if (F->getName().equals("__ubsan_handle_out_of_bounds"))
350 return true;
351
352 if (isa<UnreachableInst>(BB.getTerminator()))
353 return true;
354
355 return false;
356}
Johannes Doerfert9a132f32015-09-28 09:33:22 +0000357
358Value *polly::getConditionFromTerminator(TerminatorInst *TI) {
359 if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
360 if (BR->isUnconditional())
361 return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
362
363 return BR->getCondition();
364 }
365
366 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
367 return SI->getCondition();
368
369 return nullptr;
370}