13b11a16aSHongbin Zheng //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===// 23b11a16aSHongbin Zheng // 33b11a16aSHongbin Zheng // The LLVM Compiler Infrastructure 43b11a16aSHongbin Zheng // 53b11a16aSHongbin Zheng // This file is distributed under the University of Illinois Open Source 63b11a16aSHongbin Zheng // License. See LICENSE.TXT for details. 73b11a16aSHongbin Zheng // 83b11a16aSHongbin Zheng //===----------------------------------------------------------------------===// 93b11a16aSHongbin Zheng // 103b11a16aSHongbin Zheng // This file implements the BlockGenerator and VectorBlockGenerator classes, 113b11a16aSHongbin Zheng // which generate sequential code and vectorized code for a polyhedral 123b11a16aSHongbin Zheng // statement, respectively. 133b11a16aSHongbin Zheng // 143b11a16aSHongbin Zheng //===----------------------------------------------------------------------===// 153b11a16aSHongbin Zheng 168a846610SHongbin Zheng #include "polly/CodeGen/BlockGenerators.h" 1783628182STobias Grosser #include "polly/CodeGen/CodeGeneration.h" 18a63b2579SJohannes Doerfert #include "polly/CodeGen/IslExprBuilder.h" 1986bc93a9STobias Grosser #include "polly/CodeGen/RuntimeDebugBuilder.h" 20637bd631STobias Grosser #include "polly/Options.h" 215624d3c9STobias Grosser #include "polly/ScopInfo.h" 223b11a16aSHongbin Zheng #include "polly/Support/GICHelper.h" 2397cb813cSSebastian Pop #include "polly/Support/SCEVValidator.h" 24ecfe21b7STobias Grosser #include "polly/Support/ScopHelper.h" 25eedae763SMichael Kruse #include "polly/Support/VirtualInstruction.h" 26e71c6ab5STobias Grosser #include "llvm/Analysis/LoopInfo.h" 27f32d651dSJohannes Doerfert #include "llvm/Analysis/RegionInfo.h" 28e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolution.h" 29030237d0STobias Grosser #include "llvm/IR/IntrinsicInst.h" 30c9895067STobias Grosser #include "llvm/IR/Module.h" 313b11a16aSHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 321b9d25a4STobias Grosser #include "llvm/Transforms/Utils/Local.h" 33f32d651dSJohannes Doerfert #include "isl/aff.h" 34f32d651dSJohannes Doerfert #include "isl/ast.h" 35f32d651dSJohannes Doerfert #include "isl/ast_build.h" 36ba0d0922STobias Grosser #include "isl/set.h" 37f32d651dSJohannes Doerfert #include <deque> 38f32d651dSJohannes Doerfert 393b11a16aSHongbin Zheng using namespace llvm; 403b11a16aSHongbin Zheng using namespace polly; 413b11a16aSHongbin Zheng 42878aba49STobias Grosser static cl::opt<bool> Aligned("enable-polly-aligned", 43878aba49STobias Grosser cl::desc("Assumed aligned memory accesses."), 44878aba49STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore, 45878aba49STobias Grosser cl::cat(PollyCategory)); 463b11a16aSHongbin Zheng 471be726a4STobias Grosser bool PollyDebugPrinting; 481be726a4STobias Grosser static cl::opt<bool, true> DebugPrintingX( 4986bc93a9STobias Grosser "polly-codegen-add-debug-printing", 5086bc93a9STobias Grosser cl::desc("Add printf calls that show the values loaded/stored."), 511be726a4STobias Grosser cl::location(PollyDebugPrinting), cl::Hidden, cl::init(false), 521be726a4STobias Grosser cl::ZeroOrMore, cl::cat(PollyCategory)); 5386bc93a9STobias Grosser 54acf80064SEli Friedman BlockGenerator::BlockGenerator( 55acf80064SEli Friedman PollyIRBuilder &B, LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT, 56587f1f57STobias Grosser AllocaMapTy &ScalarMap, EscapeUsersAllocaMapTy &EscapeMap, 57587f1f57STobias Grosser ValueMapT &GlobalMap, IslExprBuilder *ExprBuilder, BasicBlock *StartBlock) 58ecff11dcSJohannes Doerfert : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT), 59587f1f57STobias Grosser EntryBB(nullptr), ScalarMap(ScalarMap), EscapeMap(EscapeMap), 60587f1f57STobias Grosser GlobalMap(GlobalMap), StartBlock(StartBlock) {} 61e71c6ab5STobias Grosser 622f1acac6STobias Grosser Value *BlockGenerator::trySynthesizeNewValue(ScopStmt &Stmt, Value *Old, 6333cb9f94STobias Grosser ValueMapT &BBMap, 6433cb9f94STobias Grosser LoopToScevMapT <S, 65bc132607STobias Grosser Loop *L) const { 6642df8d1dSJohannes Doerfert if (!SE.isSCEVable(Old->getType())) 6742df8d1dSJohannes Doerfert return nullptr; 6842df8d1dSJohannes Doerfert 6942df8d1dSJohannes Doerfert const SCEV *Scev = SE.getSCEVAtScope(Old, L); 7042df8d1dSJohannes Doerfert if (!Scev) 7142df8d1dSJohannes Doerfert return nullptr; 7242df8d1dSJohannes Doerfert 7342df8d1dSJohannes Doerfert if (isa<SCEVCouldNotCompute>(Scev)) 7442df8d1dSJohannes Doerfert return nullptr; 7542df8d1dSJohannes Doerfert 7603bcb910SSanjoy Das const SCEV *NewScev = SCEVLoopAddRecRewriter::rewrite(Scev, LTS, SE); 7709e3697fSJohannes Doerfert ValueMapT VTV; 78637b23dcSSebastian Pop VTV.insert(BBMap.begin(), BBMap.end()); 79637b23dcSSebastian Pop VTV.insert(GlobalMap.begin(), GlobalMap.end()); 80e69e1141SJohannes Doerfert 81e69e1141SJohannes Doerfert Scop &S = *Stmt.getParent(); 823f52e354SJohannes Doerfert const DataLayout &DL = S.getFunction().getParent()->getDataLayout(); 83e69e1141SJohannes Doerfert auto IP = Builder.GetInsertPoint(); 84e69e1141SJohannes Doerfert 85e69e1141SJohannes Doerfert assert(IP != Builder.GetInsertBlock()->end() && 8645e7944bSTobias Grosser "Only instructions can be insert points for SCEVExpander"); 8742df8d1dSJohannes Doerfert Value *Expanded = 88acf80064SEli Friedman expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), &*IP, &VTV, 89acf80064SEli Friedman StartBlock->getSinglePredecessor()); 90e71c6ab5STobias Grosser 91e71c6ab5STobias Grosser BBMap[Old] = Expanded; 92e71c6ab5STobias Grosser return Expanded; 93e71c6ab5STobias Grosser } 9433cb9f94STobias Grosser 952f1acac6STobias Grosser Value *BlockGenerator::getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap, 962f1acac6STobias Grosser LoopToScevMapT <S, Loop *L) const { 9733cb9f94STobias Grosser 98eedae763SMichael Kruse auto lookupGlobally = [this](Value *Old) -> Value * { 99eedae763SMichael Kruse Value *New = GlobalMap.lookup(Old); 100eedae763SMichael Kruse if (!New) 101eedae763SMichael Kruse return nullptr; 10228f8ac1dSJohannes Doerfert 103eedae763SMichael Kruse // Required by: 104eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/invariant_base_pointer_preloaded.ll 105eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/invariant_base_pointer_preloaded_different_bb.ll 106eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/invariant_base_pointer_preloaded_pass_only_needed.ll 107eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/invariant_base_pointers_preloaded.ll 108eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/loop-body-references-outer-values-3.ll 109eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/single_loop_with_loop_invariant_baseptr.ll 110eedae763SMichael Kruse // GlobalMap should be a mapping from (value in original SCoP) to (copied 111eedae763SMichael Kruse // value in generated SCoP), without intermediate mappings, which might 112eedae763SMichael Kruse // easily require transitiveness as well. 11333cb9f94STobias Grosser if (Value *NewRemapped = GlobalMap.lookup(New)) 11433cb9f94STobias Grosser New = NewRemapped; 115eedae763SMichael Kruse 116eedae763SMichael Kruse // No test case for this code. 11733cb9f94STobias Grosser if (Old->getType()->getScalarSizeInBits() < 11833cb9f94STobias Grosser New->getType()->getScalarSizeInBits()) 11933cb9f94STobias Grosser New = Builder.CreateTruncOrBitCast(New, Old->getType()); 12033cb9f94STobias Grosser 12133cb9f94STobias Grosser return New; 122eedae763SMichael Kruse }; 123eedae763SMichael Kruse 124eedae763SMichael Kruse Value *New = nullptr; 125eedae763SMichael Kruse auto VUse = VirtualUse::create(&Stmt, L, Old, true); 126eedae763SMichael Kruse switch (VUse.getKind()) { 127eedae763SMichael Kruse case VirtualUse::Block: 128eedae763SMichael Kruse // BasicBlock are constants, but the BlockGenerator copies them. 129eedae763SMichael Kruse New = BBMap.lookup(Old); 130eedae763SMichael Kruse break; 131eedae763SMichael Kruse 132eedae763SMichael Kruse case VirtualUse::Constant: 133eedae763SMichael Kruse // Used by: 134eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/reference-argument-from-non-affine-region.ll 135eedae763SMichael Kruse // Constants should not be redefined. In this case, the GlobalMap just 136eedae763SMichael Kruse // contains a mapping to the same constant, which is unnecessary, but 137eedae763SMichael Kruse // harmless. 138eedae763SMichael Kruse if ((New = lookupGlobally(Old))) 139eedae763SMichael Kruse break; 140eedae763SMichael Kruse 141eedae763SMichael Kruse assert(!BBMap.count(Old)); 142eedae763SMichael Kruse New = Old; 143eedae763SMichael Kruse break; 144eedae763SMichael Kruse 145eedae763SMichael Kruse case VirtualUse::ReadOnly: 146eedae763SMichael Kruse assert(!GlobalMap.count(Old)); 147eedae763SMichael Kruse 148eedae763SMichael Kruse // Required for: 149eedae763SMichael Kruse // * Isl/CodeGen/MemAccess/create_arrays.ll 150eedae763SMichael Kruse // * Isl/CodeGen/read-only-scalars.ll 151eedae763SMichael Kruse // * ScheduleOptimizer/pattern-matching-based-opts_10.ll 152eedae763SMichael Kruse // For some reason these reload a read-only value. The reloaded value ends 153eedae763SMichael Kruse // up in BBMap, buts its value should be identical. 154eedae763SMichael Kruse // 155eedae763SMichael Kruse // Required for: 156eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/single_loop_with_param.ll 157eedae763SMichael Kruse // The parallel subfunctions need to reference the read-only value from the 158eedae763SMichael Kruse // parent function, this is done by reloading them locally. 159eedae763SMichael Kruse if ((New = BBMap.lookup(Old))) 160eedae763SMichael Kruse break; 161eedae763SMichael Kruse 162eedae763SMichael Kruse New = Old; 163eedae763SMichael Kruse break; 164eedae763SMichael Kruse 165eedae763SMichael Kruse case VirtualUse::Synthesizable: 166eedae763SMichael Kruse // Used by: 167eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/loop-body-references-outer-values-3.ll 168eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/recomputed-srem.ll 169eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/reference-other-bb.ll 170eedae763SMichael Kruse // * Isl/CodeGen/OpenMP/two-parallel-loops-reference-outer-indvar.ll 171eedae763SMichael Kruse // For some reason synthesizable values end up in GlobalMap. Their values 172eedae763SMichael Kruse // are the same as trySynthesizeNewValue would return. The legacy 173eedae763SMichael Kruse // implementation prioritized GlobalMap, so this is what we do here as well. 174eedae763SMichael Kruse // Ideally, synthesizable values should not end up in GlobalMap. 175eedae763SMichael Kruse if ((New = lookupGlobally(Old))) 176eedae763SMichael Kruse break; 177eedae763SMichael Kruse 178eedae763SMichael Kruse // Required for: 179eedae763SMichael Kruse // * Isl/CodeGen/RuntimeDebugBuilder/combine_different_values.ll 180eedae763SMichael Kruse // * Isl/CodeGen/getNumberOfIterations.ll 181eedae763SMichael Kruse // * Isl/CodeGen/non_affine_float_compare.ll 182eedae763SMichael Kruse // * ScheduleOptimizer/pattern-matching-based-opts_10.ll 183eedae763SMichael Kruse // Ideally, synthesizable values are synthesized by trySynthesizeNewValue, 184eedae763SMichael Kruse // not precomputed (SCEVExpander has its own caching mechanism). 185eedae763SMichael Kruse // These tests fail without this, but I think trySynthesizeNewValue would 186eedae763SMichael Kruse // just re-synthesize the same instructions. 187eedae763SMichael Kruse if ((New = BBMap.lookup(Old))) 188eedae763SMichael Kruse break; 189eedae763SMichael Kruse 190eedae763SMichael Kruse New = trySynthesizeNewValue(Stmt, Old, BBMap, LTS, L); 191eedae763SMichael Kruse break; 192eedae763SMichael Kruse 193eedae763SMichael Kruse case VirtualUse::Hoisted: 194eedae763SMichael Kruse // TODO: Hoisted invariant loads should be found in GlobalMap only, but not 195eedae763SMichael Kruse // redefined locally (which will be ignored anyway). That is, the following 196eedae763SMichael Kruse // assertion should apply: assert(!BBMap.count(Old)) 197eedae763SMichael Kruse 198eedae763SMichael Kruse New = lookupGlobally(Old); 199eedae763SMichael Kruse break; 200eedae763SMichael Kruse 201eedae763SMichael Kruse case VirtualUse::Intra: 202eedae763SMichael Kruse case VirtualUse::Inter: 203eedae763SMichael Kruse assert(!GlobalMap.count(Old) && 204eedae763SMichael Kruse "Intra and inter-stmt values are never global"); 205eedae763SMichael Kruse New = BBMap.lookup(Old); 206eedae763SMichael Kruse break; 20733cb9f94STobias Grosser } 208eedae763SMichael Kruse assert(New && "Unexpected scalar dependence in region!"); 20933cb9f94STobias Grosser return New; 2103b11a16aSHongbin Zheng } 2113b11a16aSHongbin Zheng 2122f1acac6STobias Grosser void BlockGenerator::copyInstScalar(ScopStmt &Stmt, Instruction *Inst, 213bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S) { 214030237d0STobias Grosser // We do not generate debug intrinsics as we did not investigate how to 215030237d0STobias Grosser // copy them correctly. At the current state, they just crash the code 216030237d0STobias Grosser // generation as the meta-data operands are not correctly copied. 217030237d0STobias Grosser if (isa<DbgInfoIntrinsic>(Inst)) 218030237d0STobias Grosser return; 219030237d0STobias Grosser 2203b11a16aSHongbin Zheng Instruction *NewInst = Inst->clone(); 2213b11a16aSHongbin Zheng 2223b11a16aSHongbin Zheng // Replace old operands with the new ones. 22391f5b262STobias Grosser for (Value *OldOperand : Inst->operands()) { 224bc132607STobias Grosser Value *NewOperand = 2252c3ffc04SJohannes Doerfert getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForStmt(Stmt)); 2263b11a16aSHongbin Zheng 2273b11a16aSHongbin Zheng if (!NewOperand) { 228c14582f2STobias Grosser assert(!isa<StoreInst>(NewInst) && 229c14582f2STobias Grosser "Store instructions are always needed!"); 23096ab8726SReid Kleckner NewInst->deleteValue(); 2313b11a16aSHongbin Zheng return; 2323b11a16aSHongbin Zheng } 2333b11a16aSHongbin Zheng 2343b11a16aSHongbin Zheng NewInst->replaceUsesOfWith(OldOperand, NewOperand); 2353b11a16aSHongbin Zheng } 2363b11a16aSHongbin Zheng 2373b11a16aSHongbin Zheng Builder.Insert(NewInst); 2383b11a16aSHongbin Zheng BBMap[Inst] = NewInst; 2393b11a16aSHongbin Zheng 2403b11a16aSHongbin Zheng if (!NewInst->getType()->isVoidTy()) 2413b11a16aSHongbin Zheng NewInst->setName("p_" + Inst->getName()); 2423b11a16aSHongbin Zheng } 2433b11a16aSHongbin Zheng 24470131d34SMichael Kruse Value * 24570131d34SMichael Kruse BlockGenerator::generateLocationAccessed(ScopStmt &Stmt, MemAccInst Inst, 24670131d34SMichael Kruse ValueMapT &BBMap, LoopToScevMapT <S, 24770131d34SMichael Kruse isl_id_to_ast_expr *NewAccesses) { 24809214772STobias Grosser const MemoryAccess &MA = Stmt.getArrayAccessFor(Inst); 2492fa35194SMichael Kruse return generateLocationAccessed( 2502fa35194SMichael Kruse Stmt, getLoopForStmt(Stmt), 2512fa35194SMichael Kruse Inst.isNull() ? nullptr : Inst.getPointerOperand(), BBMap, LTS, 2522fa35194SMichael Kruse NewAccesses, MA.getId(), MA.getAccessValue()->getType()); 2532fa35194SMichael Kruse } 2543b11a16aSHongbin Zheng 2552fa35194SMichael Kruse Value *BlockGenerator::generateLocationAccessed( 2562fa35194SMichael Kruse ScopStmt &Stmt, Loop *L, Value *Pointer, ValueMapT &BBMap, 2572fa35194SMichael Kruse LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses, __isl_take isl_id *Id, 2582fa35194SMichael Kruse Type *ExpectedType) { 2592fa35194SMichael Kruse isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, Id); 2603b11a16aSHongbin Zheng 2612d1ed0bfSTobias Grosser if (AccessExpr) { 2622d1ed0bfSTobias Grosser AccessExpr = isl_ast_expr_address_of(AccessExpr); 26398b3ee50STobias Grosser auto Address = ExprBuilder->create(AccessExpr); 26498b3ee50STobias Grosser 26598b3ee50STobias Grosser // Cast the address of this memory access to a pointer type that has the 26698b3ee50STobias Grosser // same element type as the original access, but uses the address space of 26798b3ee50STobias Grosser // the newly generated pointer. 2682fa35194SMichael Kruse auto OldPtrTy = ExpectedType->getPointerTo(); 26998b3ee50STobias Grosser auto NewPtrTy = Address->getType(); 27098b3ee50STobias Grosser OldPtrTy = PointerType::get(OldPtrTy->getElementType(), 27198b3ee50STobias Grosser NewPtrTy->getPointerAddressSpace()); 27298b3ee50STobias Grosser 273d840fc72STobias Grosser if (OldPtrTy != NewPtrTy) 27498b3ee50STobias Grosser Address = Builder.CreateBitOrPointerCast(Address, OldPtrTy); 27598b3ee50STobias Grosser return Address; 2762d1ed0bfSTobias Grosser } 2772fa35194SMichael Kruse assert( 2782fa35194SMichael Kruse Pointer && 2792fa35194SMichael Kruse "If expression was not generated, must use the original pointer value"); 2802fa35194SMichael Kruse return getNewValue(Stmt, Pointer, BBMap, LTS, L); 2812fa35194SMichael Kruse } 2822d1ed0bfSTobias Grosser 2832fa35194SMichael Kruse Value * 2842fa35194SMichael Kruse BlockGenerator::getImplicitAddress(MemoryAccess &Access, Loop *L, 2852fa35194SMichael Kruse LoopToScevMapT <S, ValueMapT &BBMap, 2862fa35194SMichael Kruse __isl_keep isl_id_to_ast_expr *NewAccesses) { 2872fa35194SMichael Kruse if (Access.isLatestArrayKind()) 2882fa35194SMichael Kruse return generateLocationAccessed(*Access.getStatement(), L, nullptr, BBMap, 2892fa35194SMichael Kruse LTS, NewAccesses, Access.getId(), 2902fa35194SMichael Kruse Access.getAccessValue()->getType()); 2912fa35194SMichael Kruse 292587f1f57STobias Grosser return getOrCreateAlloca(Access); 2933b11a16aSHongbin Zheng } 2943b11a16aSHongbin Zheng 2952c3ffc04SJohannes Doerfert Loop *BlockGenerator::getLoopForStmt(const ScopStmt &Stmt) const { 296375cb5feSMichael Kruse auto *StmtBB = Stmt.getEntryBlock(); 2972c3ffc04SJohannes Doerfert return LI.getLoopFor(StmtBB); 298369430ffSTobias Grosser } 299369430ffSTobias Grosser 300888ab551SMichael Kruse Value *BlockGenerator::generateArrayLoad(ScopStmt &Stmt, LoadInst *Load, 301bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S, 3022d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 303c1db67e2SJohannes Doerfert if (Value *PreloadLoad = GlobalMap.lookup(Load)) 304c1db67e2SJohannes Doerfert return PreloadLoad; 305c1db67e2SJohannes Doerfert 306bc132607STobias Grosser Value *NewPointer = 30770131d34SMichael Kruse generateLocationAccessed(Stmt, Load, BBMap, LTS, NewAccesses); 30887901453SJohannes Doerfert Value *ScalarLoad = Builder.CreateAlignedLoad( 30987901453SJohannes Doerfert NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); 31086bc93a9STobias Grosser 3111be726a4STobias Grosser if (PollyDebugPrinting) 31286bc93a9STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer, 31386bc93a9STobias Grosser ": ", ScalarLoad, "\n"); 31486bc93a9STobias Grosser 3153b11a16aSHongbin Zheng return ScalarLoad; 3163b11a16aSHongbin Zheng } 3173b11a16aSHongbin Zheng 318888ab551SMichael Kruse void BlockGenerator::generateArrayStore(ScopStmt &Stmt, StoreInst *Store, 319bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S, 3202d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 321706f79abSMichael Kruse MemoryAccess &MA = Stmt.getArrayAccessFor(Store); 322706f79abSMichael Kruse isl::set AccDom = give(isl_map_domain(MA.getAccessRelation())); 323706f79abSMichael Kruse const char *Subject = isl_id_get_name(give(MA.getId()).keep()); 324706f79abSMichael Kruse 325706f79abSMichael Kruse generateConditionalExecution(Stmt, AccDom, Subject, [&, this]() { 326bc132607STobias Grosser Value *NewPointer = 32770131d34SMichael Kruse generateLocationAccessed(Stmt, Store, BBMap, LTS, NewAccesses); 328706f79abSMichael Kruse Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, 329706f79abSMichael Kruse LTS, getLoopForStmt(Stmt)); 3303b11a16aSHongbin Zheng 3311be726a4STobias Grosser if (PollyDebugPrinting) 33286bc93a9STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to ", NewPointer, 33386bc93a9STobias Grosser ": ", ValueOperand, "\n"); 33486bc93a9STobias Grosser 335c186ac7aSTobias Grosser Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment()); 336706f79abSMichael Kruse }); 3373b11a16aSHongbin Zheng } 3383b11a16aSHongbin Zheng 3395dced269SJohannes Doerfert bool BlockGenerator::canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst) { 3402c3ffc04SJohannes Doerfert Loop *L = getLoopForStmt(Stmt); 3415dced269SJohannes Doerfert return (Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) && 34211c5e079SMichael Kruse canSynthesize(Inst, *Stmt.getParent(), &SE, L); 3435dced269SJohannes Doerfert } 3445dced269SJohannes Doerfert 3452f1acac6STobias Grosser void BlockGenerator::copyInstruction(ScopStmt &Stmt, Instruction *Inst, 346bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S, 3472d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 3483b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 3493b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 3503b11a16aSHongbin Zheng if (Inst->isTerminator()) 3513b11a16aSHongbin Zheng return; 3523b11a16aSHongbin Zheng 353aff56c8aSTobias Grosser // Synthesizable statements will be generated on-demand. 3545dced269SJohannes Doerfert if (canSyntheziseInStmt(Stmt, Inst)) 355e71c6ab5STobias Grosser return; 356e71c6ab5STobias Grosser 3579646e3feSTobias Grosser if (auto *Load = dyn_cast<LoadInst>(Inst)) { 358888ab551SMichael Kruse Value *NewLoad = generateArrayLoad(Stmt, Load, BBMap, LTS, NewAccesses); 3593d94fedfSSebastian Pop // Compute NewLoad before its insertion in BBMap to make the insertion 3603d94fedfSSebastian Pop // deterministic. 361753d43f9SSebastian Pop BBMap[Load] = NewLoad; 3623b11a16aSHongbin Zheng return; 3633b11a16aSHongbin Zheng } 3643b11a16aSHongbin Zheng 3659646e3feSTobias Grosser if (auto *Store = dyn_cast<StoreInst>(Inst)) { 3660446d81eSMichael Kruse // Identified as redundant by -polly-simplify. 3670446d81eSMichael Kruse if (!Stmt.getArrayAccessOrNULLFor(Store)) 3680446d81eSMichael Kruse return; 3690446d81eSMichael Kruse 370888ab551SMichael Kruse generateArrayStore(Stmt, Store, BBMap, LTS, NewAccesses); 3713b11a16aSHongbin Zheng return; 3723b11a16aSHongbin Zheng } 3733b11a16aSHongbin Zheng 3749646e3feSTobias Grosser if (auto *PHI = dyn_cast<PHINode>(Inst)) { 375bc132607STobias Grosser copyPHIInstruction(Stmt, PHI, BBMap, LTS); 376ecff11dcSJohannes Doerfert return; 377ecff11dcSJohannes Doerfert } 378ecff11dcSJohannes Doerfert 3793f500fa2SJohannes Doerfert // Skip some special intrinsics for which we do not adjust the semantics to 3803f500fa2SJohannes Doerfert // the new schedule. All others are handled like every other instruction. 3819c0ffe3aSTobias Grosser if (isIgnoredIntrinsic(Inst)) 3823f500fa2SJohannes Doerfert return; 3833f500fa2SJohannes Doerfert 384bc132607STobias Grosser copyInstScalar(Stmt, Inst, BBMap, LTS); 3853b11a16aSHongbin Zheng } 3863b11a16aSHongbin Zheng 3879d12d8adSTobias Grosser void BlockGenerator::removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap) { 388c59b3ce0STobias Grosser auto NewBB = Builder.GetInsertBlock(); 389c59b3ce0STobias Grosser for (auto I = NewBB->rbegin(); I != NewBB->rend(); I++) { 390c59b3ce0STobias Grosser Instruction *NewInst = &*I; 3919d12d8adSTobias Grosser 3929d12d8adSTobias Grosser if (!isInstructionTriviallyDead(NewInst)) 3939d12d8adSTobias Grosser continue; 3949d12d8adSTobias Grosser 395c59b3ce0STobias Grosser for (auto Pair : BBMap) 396c59b3ce0STobias Grosser if (Pair.second == NewInst) { 397c59b3ce0STobias Grosser BBMap.erase(Pair.first); 398c59b3ce0STobias Grosser } 399c59b3ce0STobias Grosser 4009d12d8adSTobias Grosser NewInst->eraseFromParent(); 401c59b3ce0STobias Grosser I = NewBB->rbegin(); 4029d12d8adSTobias Grosser } 4039d12d8adSTobias Grosser } 4049d12d8adSTobias Grosser 405bc132607STobias Grosser void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, 4062d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 407275a1756SJohannes Doerfert assert(Stmt.isBlockStmt() && 408275a1756SJohannes Doerfert "Only block statements can be copied by the block generator"); 409275a1756SJohannes Doerfert 410275a1756SJohannes Doerfert ValueMapT BBMap; 411275a1756SJohannes Doerfert 412be9c9117SJohannes Doerfert BasicBlock *BB = Stmt.getBasicBlock(); 413bc132607STobias Grosser copyBB(Stmt, BB, BBMap, LTS, NewAccesses); 4149d12d8adSTobias Grosser removeDeadInstructions(BB, BBMap); 415275a1756SJohannes Doerfert } 416275a1756SJohannes Doerfert 417514f6efaSJohannes Doerfert BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { 418b8f58b53SDuncan P. N. Exon Smith BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), 419b8f58b53SDuncan P. N. Exon Smith &*Builder.GetInsertPoint(), &DT, &LI); 4203b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 421514f6efaSJohannes Doerfert return CopyBB; 422514f6efaSJohannes Doerfert } 4233b11a16aSHongbin Zheng 424514f6efaSJohannes Doerfert BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, 425bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S, 4262d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 427514f6efaSJohannes Doerfert BasicBlock *CopyBB = splitBB(BB); 428b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&CopyBB->front()); 4292fa35194SMichael Kruse generateScalarLoads(Stmt, LTS, BBMap, NewAccesses); 430225f0d1eSMichael Kruse 431bc132607STobias Grosser copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses); 432225f0d1eSMichael Kruse 433225f0d1eSMichael Kruse // After a basic block was copied store all scalars that escape this block in 434225f0d1eSMichael Kruse // their alloca. 4352fa35194SMichael Kruse generateScalarStores(Stmt, LTS, BBMap, NewAccesses); 436514f6efaSJohannes Doerfert return CopyBB; 437514f6efaSJohannes Doerfert } 438514f6efaSJohannes Doerfert 439514f6efaSJohannes Doerfert void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB, 440bc132607STobias Grosser ValueMapT &BBMap, LoopToScevMapT <S, 4412d1ed0bfSTobias Grosser isl_id_to_ast_expr *NewAccesses) { 442ecff11dcSJohannes Doerfert EntryBB = &CopyBB->getParent()->getEntryBlock(); 443ecff11dcSJohannes Doerfert 44491f5b262STobias Grosser for (Instruction &Inst : *BB) 445bc132607STobias Grosser copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses); 446ecff11dcSJohannes Doerfert } 447ecff11dcSJohannes Doerfert 448800e17a7SJohannes Doerfert Value *BlockGenerator::getOrCreateAlloca(const MemoryAccess &Access) { 449587f1f57STobias Grosser assert(!Access.isLatestArrayKind() && "Trying to get alloca for array kind"); 4503216f854STobias Grosser 451587f1f57STobias Grosser return getOrCreateAlloca(Access.getLatestScopArrayInfo()); 452a4f0988dSTobias Grosser } 453a4f0988dSTobias Grosser 454a4f0988dSTobias Grosser Value *BlockGenerator::getOrCreateAlloca(const ScopArrayInfo *Array) { 4553216f854STobias Grosser assert(!Array->isArrayKind() && "Trying to get alloca for array kind"); 4563216f854STobias Grosser 457587f1f57STobias Grosser auto &Addr = ScalarMap[Array]; 458587f1f57STobias Grosser 459587f1f57STobias Grosser if (Addr) { 460587f1f57STobias Grosser // Allow allocas to be (temporarily) redirected once by adding a new 461587f1f57STobias Grosser // old-alloca-addr to new-addr mapping to GlobalMap. This funcitionality 462587f1f57STobias Grosser // is used for example by the OpenMP code generation where a first use 463587f1f57STobias Grosser // of a scalar while still in the host code allocates a normal alloca with 464587f1f57STobias Grosser // getOrCreateAlloca. When the values of this scalar are accessed during 465587f1f57STobias Grosser // the generation of the parallel subfunction, these values are copied over 466587f1f57STobias Grosser // to the parallel subfunction and each request for a scalar alloca slot 467587f1f57STobias Grosser // must be forwared to the temporary in-subfunction slot. This mapping is 468587f1f57STobias Grosser // removed when the subfunction has been generated and again normal host 469682c5114STobias Grosser // code is generated. Due to the following reasons it is not possible to 470682c5114STobias Grosser // perform the GlobalMap lookup right after creating the alloca below, but 471682c5114STobias Grosser // instead we need to check GlobalMap at each call to getOrCreateAlloca: 472682c5114STobias Grosser // 473682c5114STobias Grosser // 1) GlobalMap may be changed multiple times (for each parallel loop), 474682c5114STobias Grosser // 2) The temporary mapping is commonly only known after the initial 475682c5114STobias Grosser // alloca has already been generated, and 476682c5114STobias Grosser // 3) The original alloca value must be restored after leaving the 477682c5114STobias Grosser // sub-function. 478587f1f57STobias Grosser if (Value *NewAddr = GlobalMap.lookup(&*Addr)) 479587f1f57STobias Grosser return NewAddr; 480587f1f57STobias Grosser return Addr; 481587f1f57STobias Grosser } 482587f1f57STobias Grosser 483587f1f57STobias Grosser Type *Ty = Array->getElementType(); 484587f1f57STobias Grosser Value *ScalarBase = Array->getBasePtr(); 485587f1f57STobias Grosser std::string NameExt; 486a535dff4STobias Grosser if (Array->isPHIKind()) 487587f1f57STobias Grosser NameExt = ".phiops"; 488f8d55f7eSTobias Grosser else 489587f1f57STobias Grosser NameExt = ".s2a"; 490587f1f57STobias Grosser 4917b5a4dfdSTobias Grosser const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout(); 492b3e30c32SMatt Arsenault 493b3e30c32SMatt Arsenault Addr = new AllocaInst(Ty, DL.getAllocaAddrSpace(), 494b3e30c32SMatt Arsenault ScalarBase->getName() + NameExt); 495587f1f57STobias Grosser EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); 496587f1f57STobias Grosser Addr->insertBefore(&*EntryBB->getFirstInsertionPt()); 497587f1f57STobias Grosser 498587f1f57STobias Grosser return Addr; 499f8d55f7eSTobias Grosser } 500f8d55f7eSTobias Grosser 501587f1f57STobias Grosser void BlockGenerator::handleOutsideUsers(const Scop &S, ScopArrayInfo *Array) { 502587f1f57STobias Grosser Instruction *Inst = cast<Instruction>(Array->getBasePtr()); 503b79a67dfSTobias Grosser 5042985400aSTobias Grosser // If there are escape users we get the alloca for this instruction and put it 5052985400aSTobias Grosser // in the EscapeMap for later finalization. Lastly, if the instruction was 5062985400aSTobias Grosser // copied multiple times we already did this and can exit. 507acb6ade7SMichael Kruse if (EscapeMap.count(Inst)) 508acb6ade7SMichael Kruse return; 509e69e1141SJohannes Doerfert 510ecff11dcSJohannes Doerfert EscapeUserVectorTy EscapeUsers; 511ecff11dcSJohannes Doerfert for (User *U : Inst->users()) { 512ecff11dcSJohannes Doerfert 513ecff11dcSJohannes Doerfert // Non-instruction user will never escape. 514ecff11dcSJohannes Doerfert Instruction *UI = dyn_cast<Instruction>(U); 515ecff11dcSJohannes Doerfert if (!UI) 516ecff11dcSJohannes Doerfert continue; 517ecff11dcSJohannes Doerfert 518952b5304SJohannes Doerfert if (S.contains(UI)) 519ecff11dcSJohannes Doerfert continue; 520ecff11dcSJohannes Doerfert 521ecff11dcSJohannes Doerfert EscapeUsers.push_back(UI); 522ecff11dcSJohannes Doerfert } 523ecff11dcSJohannes Doerfert 524ecff11dcSJohannes Doerfert // Exit if no escape uses were found. 525ecff11dcSJohannes Doerfert if (EscapeUsers.empty()) 526ecff11dcSJohannes Doerfert return; 527ecff11dcSJohannes Doerfert 528ecff11dcSJohannes Doerfert // Get or create an escape alloca for this instruction. 529587f1f57STobias Grosser auto *ScalarAddr = getOrCreateAlloca(Array); 530ecff11dcSJohannes Doerfert 531ecff11dcSJohannes Doerfert // Remember that this instruction has escape uses and the escape alloca. 532ecff11dcSJohannes Doerfert EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers)); 533ecff11dcSJohannes Doerfert } 534ecff11dcSJohannes Doerfert 5352fa35194SMichael Kruse void BlockGenerator::generateScalarLoads( 5362fa35194SMichael Kruse ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, 5372fa35194SMichael Kruse __isl_keep isl_id_to_ast_expr *NewAccesses) { 538225f0d1eSMichael Kruse for (MemoryAccess *MA : Stmt) { 5392fa35194SMichael Kruse if (MA->isOriginalArrayKind() || MA->isWrite()) 540ecff11dcSJohannes Doerfert continue; 541ecff11dcSJohannes Doerfert 54277394f13SMichael Kruse #ifndef NDEBUG 54377394f13SMichael Kruse auto *StmtDom = Stmt.getDomain(); 54477394f13SMichael Kruse auto *AccDom = isl_map_domain(MA->getAccessRelation()); 54577394f13SMichael Kruse assert(isl_set_is_subset(StmtDom, AccDom) && 54677394f13SMichael Kruse "Scalar must be loaded in all statement instances"); 54777394f13SMichael Kruse isl_set_free(StmtDom); 54877394f13SMichael Kruse isl_set_free(AccDom); 54977394f13SMichael Kruse #endif 55077394f13SMichael Kruse 5512fa35194SMichael Kruse auto *Address = 5522fa35194SMichael Kruse getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, NewAccesses); 553eac9726eSMichael Kruse assert((!isa<Instruction>(Address) || 554eac9726eSMichael Kruse DT.dominates(cast<Instruction>(Address)->getParent(), 555eac9726eSMichael Kruse Builder.GetInsertBlock())) && 556eac9726eSMichael Kruse "Domination violation"); 557583be06fSTobias Grosser BBMap[MA->getAccessValue()] = 5582fc50df9STobias Grosser Builder.CreateLoad(Address, Address->getName() + ".reload"); 559ecff11dcSJohannes Doerfert } 560ecff11dcSJohannes Doerfert } 561ecff11dcSJohannes Doerfert 562706f79abSMichael Kruse Value *BlockGenerator::buildContainsCondition(ScopStmt &Stmt, 563706f79abSMichael Kruse const isl::set &Subdomain) { 564706f79abSMichael Kruse isl::ast_build AstBuild = give(isl_ast_build_copy(Stmt.getAstBuild())); 565706f79abSMichael Kruse isl::set Domain = give(Stmt.getDomain()); 566706f79abSMichael Kruse 5676b6ac900STobias Grosser isl::union_map USchedule = AstBuild.get_schedule(); 5686b6ac900STobias Grosser USchedule = USchedule.intersect_domain(Domain); 569706f79abSMichael Kruse 5706b6ac900STobias Grosser assert(!USchedule.is_empty()); 5716b6ac900STobias Grosser isl::map Schedule = isl::map::from_union_map(USchedule); 572706f79abSMichael Kruse 5736b6ac900STobias Grosser isl::set ScheduledDomain = Schedule.range(); 5746b6ac900STobias Grosser isl::set ScheduledSet = Subdomain.apply(Schedule); 575706f79abSMichael Kruse 5766b6ac900STobias Grosser isl::ast_build RestrictedBuild = AstBuild.restrict(ScheduledDomain); 5776b6ac900STobias Grosser 5786b6ac900STobias Grosser isl::ast_expr IsInSet = RestrictedBuild.expr_from(ScheduledSet); 579706f79abSMichael Kruse Value *IsInSetExpr = ExprBuilder->create(IsInSet.copy()); 580706f79abSMichael Kruse IsInSetExpr = Builder.CreateICmpNE( 581706f79abSMichael Kruse IsInSetExpr, ConstantInt::get(IsInSetExpr->getType(), 0)); 582706f79abSMichael Kruse 583706f79abSMichael Kruse return IsInSetExpr; 584706f79abSMichael Kruse } 585706f79abSMichael Kruse 586706f79abSMichael Kruse void BlockGenerator::generateConditionalExecution( 587706f79abSMichael Kruse ScopStmt &Stmt, const isl::set &Subdomain, StringRef Subject, 588706f79abSMichael Kruse const std::function<void()> &GenThenFunc) { 589706f79abSMichael Kruse isl::set StmtDom = give(Stmt.getDomain()); 590706f79abSMichael Kruse 591706f79abSMichael Kruse // Don't call GenThenFunc if it is never executed. An ast index expression 592706f79abSMichael Kruse // might not be defined in this case. 5936b6ac900STobias Grosser if (Subdomain.is_empty()) 594706f79abSMichael Kruse return; 595706f79abSMichael Kruse 596706f79abSMichael Kruse // If the condition is a tautology, don't generate a condition around the 597706f79abSMichael Kruse // code. 598*f51decb5STobias Grosser bool IsPartialWrite = 599*f51decb5STobias Grosser !StmtDom.intersect_params(give(Stmt.getParent()->getContext())) 600*f51decb5STobias Grosser .is_subset(Subdomain); 601*f51decb5STobias Grosser if (!IsPartialWrite) { 602706f79abSMichael Kruse GenThenFunc(); 603706f79abSMichael Kruse return; 604706f79abSMichael Kruse } 605706f79abSMichael Kruse 606706f79abSMichael Kruse // Generate the condition. 607706f79abSMichael Kruse Value *Cond = buildContainsCondition(Stmt, Subdomain); 608706f79abSMichael Kruse BasicBlock *HeadBlock = Builder.GetInsertBlock(); 609706f79abSMichael Kruse StringRef BlockName = HeadBlock->getName(); 610706f79abSMichael Kruse 611706f79abSMichael Kruse // Generate the conditional block. 612706f79abSMichael Kruse SplitBlockAndInsertIfThen(Cond, &*Builder.GetInsertPoint(), false, nullptr, 613706f79abSMichael Kruse &DT, &LI); 614706f79abSMichael Kruse BranchInst *Branch = cast<BranchInst>(HeadBlock->getTerminator()); 615706f79abSMichael Kruse BasicBlock *ThenBlock = Branch->getSuccessor(0); 616706f79abSMichael Kruse BasicBlock *TailBlock = Branch->getSuccessor(1); 617706f79abSMichael Kruse 618706f79abSMichael Kruse // Assign descriptive names. 619706f79abSMichael Kruse if (auto *CondInst = dyn_cast<Instruction>(Cond)) 620706f79abSMichael Kruse CondInst->setName("polly." + Subject + ".cond"); 621706f79abSMichael Kruse ThenBlock->setName(BlockName + "." + Subject + ".partial"); 622706f79abSMichael Kruse TailBlock->setName(BlockName + ".cont"); 623706f79abSMichael Kruse 624706f79abSMichael Kruse // Put the client code into the conditional block and continue in the merge 625706f79abSMichael Kruse // block afterwards. 626706f79abSMichael Kruse Builder.SetInsertPoint(ThenBlock, ThenBlock->getFirstInsertionPt()); 627706f79abSMichael Kruse GenThenFunc(); 628706f79abSMichael Kruse Builder.SetInsertPoint(TailBlock, TailBlock->getFirstInsertionPt()); 629706f79abSMichael Kruse } 630706f79abSMichael Kruse 6312fa35194SMichael Kruse void BlockGenerator::generateScalarStores( 6322fa35194SMichael Kruse ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, 6332fa35194SMichael Kruse __isl_keep isl_id_to_ast_expr *NewAccesses) { 634f2cdd144STobias Grosser Loop *L = LI.getLoopFor(Stmt.getBasicBlock()); 635ecff11dcSJohannes Doerfert 63621a059afSTobias Grosser assert(Stmt.isBlockStmt() && 63721a059afSTobias Grosser "Region statements need to use the generateScalarStores() function in " 63821a059afSTobias Grosser "the RegionGenerator"); 639ecff11dcSJohannes Doerfert 640ecff11dcSJohannes Doerfert for (MemoryAccess *MA : Stmt) { 6412fa35194SMichael Kruse if (MA->isOriginalArrayKind() || MA->isRead()) 642ecff11dcSJohannes Doerfert continue; 643ecff11dcSJohannes Doerfert 644706f79abSMichael Kruse isl::set AccDom = give(isl_map_domain(MA->getAccessRelation())); 645706f79abSMichael Kruse const char *Subject = isl_id_get_name(give(MA->getId()).keep()); 64677394f13SMichael Kruse 647706f79abSMichael Kruse generateConditionalExecution(Stmt, AccDom, Subject, [&, this, MA]() { 648d86f2157SJohannes Doerfert Value *Val = MA->getAccessValue(); 649ee6a4fc6SMichael Kruse if (MA->isAnyPHIKind()) { 650706f79abSMichael Kruse assert( 651706f79abSMichael Kruse MA->getIncoming().size() >= 1 && 652ee6a4fc6SMichael Kruse "Block statements have exactly one exiting block, or multiple but " 653ee6a4fc6SMichael Kruse "with same incoming block and value"); 654ee6a4fc6SMichael Kruse assert(std::all_of(MA->getIncoming().begin(), MA->getIncoming().end(), 655ee6a4fc6SMichael Kruse [&](std::pair<BasicBlock *, Value *> p) -> bool { 656ee6a4fc6SMichael Kruse return p.first == Stmt.getBasicBlock(); 657ee6a4fc6SMichael Kruse }) && 658ee6a4fc6SMichael Kruse "Incoming block must be statement's block"); 659ee6a4fc6SMichael Kruse Val = MA->getIncoming()[0].second; 660ee6a4fc6SMichael Kruse } 661706f79abSMichael Kruse auto Address = getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, 662706f79abSMichael Kruse NewAccesses); 663d86f2157SJohannes Doerfert 664f2cdd144STobias Grosser Val = getNewValue(Stmt, Val, BBMap, LTS, L); 665eac9726eSMichael Kruse assert((!isa<Instruction>(Val) || 666eac9726eSMichael Kruse DT.dominates(cast<Instruction>(Val)->getParent(), 667eac9726eSMichael Kruse Builder.GetInsertBlock())) && 668eac9726eSMichael Kruse "Domination violation"); 669eac9726eSMichael Kruse assert((!isa<Instruction>(Address) || 670eac9726eSMichael Kruse DT.dominates(cast<Instruction>(Address)->getParent(), 671eac9726eSMichael Kruse Builder.GetInsertBlock())) && 672eac9726eSMichael Kruse "Domination violation"); 67392245228STobias Grosser Builder.CreateStore(Val, Address); 674706f79abSMichael Kruse 675706f79abSMichael Kruse }); 676ecff11dcSJohannes Doerfert } 677ecff11dcSJohannes Doerfert } 678ecff11dcSJohannes Doerfert 679c0091a77STobias Grosser void BlockGenerator::createScalarInitialization(Scop &S) { 680ef74443cSJohannes Doerfert BasicBlock *ExitBB = S.getExit(); 681acf80064SEli Friedman BasicBlock *PreEntryBB = S.getEnteringBlock(); 682188542fdSJohannes Doerfert 683acf80064SEli Friedman Builder.SetInsertPoint(&*StartBlock->begin()); 684ecff11dcSJohannes Doerfert 685d7754a12SRoman Gareev for (auto &Array : S.arrays()) { 686c0091a77STobias Grosser if (Array->getNumberOfDimensions() != 0) 687c0091a77STobias Grosser continue; 688a535dff4STobias Grosser if (Array->isPHIKind()) { 689c0091a77STobias Grosser // For PHI nodes, the only values we need to store are the ones that 690c0091a77STobias Grosser // reach the PHI node from outside the region. In general there should 691c0091a77STobias Grosser // only be one such incoming edge and this edge should enter through 692acf80064SEli Friedman // 'PreEntryBB'. 693c0091a77STobias Grosser auto PHI = cast<PHINode>(Array->getBasePtr()); 694ecff11dcSJohannes Doerfert 695c0091a77STobias Grosser for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++) 696acf80064SEli Friedman if (!S.contains(*BI) && *BI != PreEntryBB) 697c0091a77STobias Grosser llvm_unreachable("Incoming edges from outside the scop should always " 698acf80064SEli Friedman "come from PreEntryBB"); 699c0091a77STobias Grosser 700acf80064SEli Friedman int Idx = PHI->getBasicBlockIndex(PreEntryBB); 701c0091a77STobias Grosser if (Idx < 0) 702ecff11dcSJohannes Doerfert continue; 703ecff11dcSJohannes Doerfert 704c0091a77STobias Grosser Value *ScalarValue = PHI->getIncomingValue(Idx); 705ecff11dcSJohannes Doerfert 706587f1f57STobias Grosser Builder.CreateStore(ScalarValue, getOrCreateAlloca(Array)); 707c0091a77STobias Grosser continue; 708c0091a77STobias Grosser } 709c0091a77STobias Grosser 710c0091a77STobias Grosser auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); 711c0091a77STobias Grosser 712952b5304SJohannes Doerfert if (Inst && S.contains(Inst)) 713c0091a77STobias Grosser continue; 714c0091a77STobias Grosser 715188542fdSJohannes Doerfert // PHI nodes that are not marked as such in their SAI object are either exit 716188542fdSJohannes Doerfert // PHI nodes we model as common scalars but without initialization, or 717188542fdSJohannes Doerfert // incoming phi nodes that need to be initialized. Check if the first is the 718188542fdSJohannes Doerfert // case for Inst and do not create and initialize memory if so. 719188542fdSJohannes Doerfert if (auto *PHI = dyn_cast_or_null<PHINode>(Inst)) 720188542fdSJohannes Doerfert if (!S.hasSingleExitEdge() && PHI->getBasicBlockIndex(ExitBB) >= 0) 721717b8667SJohannes Doerfert continue; 722717b8667SJohannes Doerfert 723587f1f57STobias Grosser Builder.CreateStore(Array->getBasePtr(), getOrCreateAlloca(Array)); 724ecff11dcSJohannes Doerfert } 725ecff11dcSJohannes Doerfert } 726ecff11dcSJohannes Doerfert 727ef74443cSJohannes Doerfert void BlockGenerator::createScalarFinalization(Scop &S) { 728ecff11dcSJohannes Doerfert // The exit block of the __unoptimized__ region. 729ef74443cSJohannes Doerfert BasicBlock *ExitBB = S.getExitingBlock(); 730ecff11dcSJohannes Doerfert // The merge block __just after__ the region and the optimized region. 731ef74443cSJohannes Doerfert BasicBlock *MergeBB = S.getExit(); 732ecff11dcSJohannes Doerfert 733ecff11dcSJohannes Doerfert // The exit block of the __optimized__ region. 734ecff11dcSJohannes Doerfert BasicBlock *OptExitBB = *(pred_begin(MergeBB)); 735ecff11dcSJohannes Doerfert if (OptExitBB == ExitBB) 736ecff11dcSJohannes Doerfert OptExitBB = *(++pred_begin(MergeBB)); 737ecff11dcSJohannes Doerfert 738ecff11dcSJohannes Doerfert Builder.SetInsertPoint(OptExitBB->getTerminator()); 739ecff11dcSJohannes Doerfert for (const auto &EscapeMapping : EscapeMap) { 740ecff11dcSJohannes Doerfert // Extract the escaping instruction and the escaping users as well as the 741ecff11dcSJohannes Doerfert // alloca the instruction was demoted to. 742fbde4355SMichael Kruse Instruction *EscapeInst = EscapeMapping.first; 743fbde4355SMichael Kruse const auto &EscapeMappingValue = EscapeMapping.second; 744ecff11dcSJohannes Doerfert const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second; 74564c0ff41STobias Grosser Value *ScalarAddr = EscapeMappingValue.first; 746ecff11dcSJohannes Doerfert 747ecff11dcSJohannes Doerfert // Reload the demoted instruction in the optimized version of the SCoP. 748d8b6ad25SJohannes Doerfert Value *EscapeInstReload = 749ecff11dcSJohannes Doerfert Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload"); 750d8b6ad25SJohannes Doerfert EscapeInstReload = 751d8b6ad25SJohannes Doerfert Builder.CreateBitOrPointerCast(EscapeInstReload, EscapeInst->getType()); 752ecff11dcSJohannes Doerfert 753ecff11dcSJohannes Doerfert // Create the merge PHI that merges the optimized and unoptimized version. 754ecff11dcSJohannes Doerfert PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2, 755ecff11dcSJohannes Doerfert EscapeInst->getName() + ".merge"); 756b8f58b53SDuncan P. N. Exon Smith MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); 757ecff11dcSJohannes Doerfert 758ecff11dcSJohannes Doerfert // Add the respective values to the merge PHI. 759ecff11dcSJohannes Doerfert MergePHI->addIncoming(EscapeInstReload, OptExitBB); 760ecff11dcSJohannes Doerfert MergePHI->addIncoming(EscapeInst, ExitBB); 761ecff11dcSJohannes Doerfert 762ecff11dcSJohannes Doerfert // The information of scalar evolution about the escaping instruction needs 763ecff11dcSJohannes Doerfert // to be revoked so the new merged instruction will be used. 764ecff11dcSJohannes Doerfert if (SE.isSCEVable(EscapeInst->getType())) 765ecff11dcSJohannes Doerfert SE.forgetValue(EscapeInst); 766ecff11dcSJohannes Doerfert 767ecff11dcSJohannes Doerfert // Replace all uses of the demoted instruction with the merge PHI. 768ecff11dcSJohannes Doerfert for (Instruction *EUser : EscapeUsers) 769ecff11dcSJohannes Doerfert EUser->replaceUsesOfWith(EscapeInst, MergePHI); 770ecff11dcSJohannes Doerfert } 771ecff11dcSJohannes Doerfert } 772ecff11dcSJohannes Doerfert 773ffa2446fSTobias Grosser void BlockGenerator::findOutsideUsers(Scop &S) { 774d7754a12SRoman Gareev for (auto &Array : S.arrays()) { 775ffa2446fSTobias Grosser 776ffa2446fSTobias Grosser if (Array->getNumberOfDimensions() != 0) 777ffa2446fSTobias Grosser continue; 778ffa2446fSTobias Grosser 779a535dff4STobias Grosser if (Array->isPHIKind()) 780ffa2446fSTobias Grosser continue; 781ffa2446fSTobias Grosser 782ffa2446fSTobias Grosser auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); 783ffa2446fSTobias Grosser 784ffa2446fSTobias Grosser if (!Inst) 785ffa2446fSTobias Grosser continue; 786ffa2446fSTobias Grosser 787ffa2446fSTobias Grosser // Scop invariant hoisting moves some of the base pointers out of the scop. 788ffa2446fSTobias Grosser // We can ignore these, as the invariant load hoisting already registers the 789ffa2446fSTobias Grosser // relevant outside users. 790952b5304SJohannes Doerfert if (!S.contains(Inst)) 791ffa2446fSTobias Grosser continue; 792ffa2446fSTobias Grosser 793587f1f57STobias Grosser handleOutsideUsers(S, Array); 794ffa2446fSTobias Grosser } 795ffa2446fSTobias Grosser } 796ffa2446fSTobias Grosser 79727d742daSTobias Grosser void BlockGenerator::createExitPHINodeMerges(Scop &S) { 79827d742daSTobias Grosser if (S.hasSingleExitEdge()) 79927d742daSTobias Grosser return; 80027d742daSTobias Grosser 801ef74443cSJohannes Doerfert auto *ExitBB = S.getExitingBlock(); 802ef74443cSJohannes Doerfert auto *MergeBB = S.getExit(); 80327d742daSTobias Grosser auto *AfterMergeBB = MergeBB->getSingleSuccessor(); 80427d742daSTobias Grosser BasicBlock *OptExitBB = *(pred_begin(MergeBB)); 80527d742daSTobias Grosser if (OptExitBB == ExitBB) 80627d742daSTobias Grosser OptExitBB = *(++pred_begin(MergeBB)); 80727d742daSTobias Grosser 80827d742daSTobias Grosser Builder.SetInsertPoint(OptExitBB->getTerminator()); 80927d742daSTobias Grosser 810d7754a12SRoman Gareev for (auto &SAI : S.arrays()) { 81127d742daSTobias Grosser auto *Val = SAI->getBasePtr(); 81227d742daSTobias Grosser 813faedfcbfSMichael Kruse // Only Value-like scalars need a merge PHI. Exit block PHIs receive either 814faedfcbfSMichael Kruse // the original PHI's value or the reloaded incoming values from the 815faedfcbfSMichael Kruse // generated code. An llvm::Value is merged between the original code's 816faedfcbfSMichael Kruse // value or the generated one. 817fa53c86dSMichael Kruse if (!SAI->isExitPHIKind()) 818faedfcbfSMichael Kruse continue; 819faedfcbfSMichael Kruse 82027d742daSTobias Grosser PHINode *PHI = dyn_cast<PHINode>(Val); 82127d742daSTobias Grosser if (!PHI) 82227d742daSTobias Grosser continue; 82327d742daSTobias Grosser 824a3f6edaeSTobias Grosser if (PHI->getParent() != AfterMergeBB) 82527d742daSTobias Grosser continue; 82627d742daSTobias Grosser 82727d742daSTobias Grosser std::string Name = PHI->getName(); 828587f1f57STobias Grosser Value *ScalarAddr = getOrCreateAlloca(SAI); 82927d742daSTobias Grosser Value *Reload = Builder.CreateLoad(ScalarAddr, Name + ".ph.final_reload"); 83027d742daSTobias Grosser Reload = Builder.CreateBitOrPointerCast(Reload, PHI->getType()); 83127d742daSTobias Grosser Value *OriginalValue = PHI->getIncomingValueForBlock(MergeBB); 832faedfcbfSMichael Kruse assert((!isa<Instruction>(OriginalValue) || 833faedfcbfSMichael Kruse cast<Instruction>(OriginalValue)->getParent() != MergeBB) && 834faedfcbfSMichael Kruse "Original value must no be one we just generated."); 83527d742daSTobias Grosser auto *MergePHI = PHINode::Create(PHI->getType(), 2, Name + ".ph.merge"); 836b8f58b53SDuncan P. N. Exon Smith MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); 83727d742daSTobias Grosser MergePHI->addIncoming(Reload, OptExitBB); 83827d742daSTobias Grosser MergePHI->addIncoming(OriginalValue, ExitBB); 83927d742daSTobias Grosser int Idx = PHI->getBasicBlockIndex(MergeBB); 84027d742daSTobias Grosser PHI->setIncomingValue(Idx, MergePHI); 84127d742daSTobias Grosser } 84227d742daSTobias Grosser } 84327d742daSTobias Grosser 8441c184409STobias Grosser void BlockGenerator::invalidateScalarEvolution(Scop &S) { 8451c184409STobias Grosser for (auto &Stmt : S) 846b3224adfSRoman Gareev if (Stmt.isCopyStmt()) 847b3224adfSRoman Gareev continue; 848b3224adfSRoman Gareev else if (Stmt.isBlockStmt()) 8491c184409STobias Grosser for (auto &Inst : *Stmt.getBasicBlock()) 8501c184409STobias Grosser SE.forgetValue(&Inst); 8511c184409STobias Grosser else if (Stmt.isRegionStmt()) 8521c184409STobias Grosser for (auto *BB : Stmt.getRegion()->blocks()) 8531c184409STobias Grosser for (auto &Inst : *BB) 8541c184409STobias Grosser SE.forgetValue(&Inst); 8551c184409STobias Grosser else 8561c184409STobias Grosser llvm_unreachable("Unexpected statement type found"); 8571aad76c1SMichael Kruse 8581aad76c1SMichael Kruse // Invalidate SCEV of loops surrounding the EscapeUsers. 8591aad76c1SMichael Kruse for (const auto &EscapeMapping : EscapeMap) { 8601aad76c1SMichael Kruse const EscapeUserVectorTy &EscapeUsers = EscapeMapping.second.second; 8611aad76c1SMichael Kruse for (Instruction *EUser : EscapeUsers) { 8621aad76c1SMichael Kruse if (Loop *L = LI.getLoopFor(EUser->getParent())) 8631aad76c1SMichael Kruse while (L) { 8641aad76c1SMichael Kruse SE.forgetLoop(L); 8651aad76c1SMichael Kruse L = L->getParentLoop(); 8661aad76c1SMichael Kruse } 8671aad76c1SMichael Kruse } 8681aad76c1SMichael Kruse } 8691c184409STobias Grosser } 8701c184409STobias Grosser 871ffa2446fSTobias Grosser void BlockGenerator::finalizeSCoP(Scop &S) { 872ffa2446fSTobias Grosser findOutsideUsers(S); 873c0091a77STobias Grosser createScalarInitialization(S); 87427d742daSTobias Grosser createExitPHINodeMerges(S); 875ef74443cSJohannes Doerfert createScalarFinalization(S); 8761c184409STobias Grosser invalidateScalarEvolution(S); 8773b11a16aSHongbin Zheng } 8783b11a16aSHongbin Zheng 879be9c9117SJohannes Doerfert VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen, 880be9c9117SJohannes Doerfert std::vector<LoopToScevMapT> &VLTS, 881be9c9117SJohannes Doerfert isl_map *Schedule) 882bc132607STobias Grosser : BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) { 883a00a0291SSebastian Pop assert(Schedule && "No statement domain provided"); 8843b11a16aSHongbin Zheng } 8853b11a16aSHongbin Zheng 8862f1acac6STobias Grosser Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, Value *Old, 887e602a076STobias Grosser ValueMapT &VectorMap, 888e602a076STobias Grosser VectorValueMapT &ScalarMaps, 889e602a076STobias Grosser Loop *L) { 890fe11e287SHongbin Zheng if (Value *NewValue = VectorMap.lookup(Old)) 891fe11e287SHongbin Zheng return NewValue; 8923b11a16aSHongbin Zheng 8933b11a16aSHongbin Zheng int Width = getVectorWidth(); 8943b11a16aSHongbin Zheng 8953b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); 8963b11a16aSHongbin Zheng 8973b11a16aSHongbin Zheng for (int Lane = 0; Lane < Width; Lane++) 898c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 899bc132607STobias Grosser Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L), 9007242ad92STobias Grosser Builder.getInt32(Lane)); 9013b11a16aSHongbin Zheng 9023b11a16aSHongbin Zheng VectorMap[Old] = Vector; 9033b11a16aSHongbin Zheng 9043b11a16aSHongbin Zheng return Vector; 9053b11a16aSHongbin Zheng } 9063b11a16aSHongbin Zheng 9073b11a16aSHongbin Zheng Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { 9083b11a16aSHongbin Zheng PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); 9093b11a16aSHongbin Zheng assert(PointerTy && "PointerType expected"); 9103b11a16aSHongbin Zheng 9113b11a16aSHongbin Zheng Type *ScalarType = PointerTy->getElementType(); 9123b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(ScalarType, Width); 9133b11a16aSHongbin Zheng 9143b11a16aSHongbin Zheng return PointerType::getUnqual(VectorType); 9153b11a16aSHongbin Zheng } 9163b11a16aSHongbin Zheng 917be9c9117SJohannes Doerfert Value *VectorBlockGenerator::generateStrideOneLoad( 9182f1acac6STobias Grosser ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, 9192d1ed0bfSTobias Grosser __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) { 9200dd463faSTobias Grosser unsigned VectorWidth = getVectorWidth(); 9219646e3feSTobias Grosser auto *Pointer = Load->getPointerOperand(); 9220dd463faSTobias Grosser Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); 9230dd463faSTobias Grosser unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; 9240dd463faSTobias Grosser 9258f25b0cbSMichael Kruse Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[Offset], 926bc132607STobias Grosser VLTS[Offset], NewAccesses); 927c14582f2STobias Grosser Value *VectorPtr = 928c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 929c14582f2STobias Grosser LoadInst *VecLoad = 930c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); 9313b11a16aSHongbin Zheng if (!Aligned) 9323b11a16aSHongbin Zheng VecLoad->setAlignment(8); 9333b11a16aSHongbin Zheng 9340dd463faSTobias Grosser if (NegativeStride) { 9350dd463faSTobias Grosser SmallVector<Constant *, 16> Indices; 9360dd463faSTobias Grosser for (int i = VectorWidth - 1; i >= 0; i--) 9370dd463faSTobias Grosser Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); 9380dd463faSTobias Grosser Constant *SV = llvm::ConstantVector::get(Indices); 9390dd463faSTobias Grosser Value *RevVecLoad = Builder.CreateShuffleVector( 9400dd463faSTobias Grosser VecLoad, VecLoad, SV, Load->getName() + "_reverse"); 9410dd463faSTobias Grosser return RevVecLoad; 9420dd463faSTobias Grosser } 9430dd463faSTobias Grosser 9443b11a16aSHongbin Zheng return VecLoad; 9453b11a16aSHongbin Zheng } 9463b11a16aSHongbin Zheng 9472d1ed0bfSTobias Grosser Value *VectorBlockGenerator::generateStrideZeroLoad( 9482f1acac6STobias Grosser ScopStmt &Stmt, LoadInst *Load, ValueMapT &BBMap, 9492d1ed0bfSTobias Grosser __isl_keep isl_id_to_ast_expr *NewAccesses) { 9509646e3feSTobias Grosser auto *Pointer = Load->getPointerOperand(); 9513b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, 1); 95270131d34SMichael Kruse Value *NewPointer = 95370131d34SMichael Kruse generateLocationAccessed(Stmt, Load, BBMap, VLTS[0], NewAccesses); 9543b11a16aSHongbin Zheng Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, 9553b11a16aSHongbin Zheng Load->getName() + "_p_vec_p"); 956c14582f2STobias Grosser LoadInst *ScalarLoad = 957c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); 9583b11a16aSHongbin Zheng 9593b11a16aSHongbin Zheng if (!Aligned) 9603b11a16aSHongbin Zheng ScalarLoad->setAlignment(8); 9613b11a16aSHongbin Zheng 962c14582f2STobias Grosser Constant *SplatVector = Constant::getNullValue( 963c14582f2STobias Grosser VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 9643b11a16aSHongbin Zheng 965c14582f2STobias Grosser Value *VectorLoad = Builder.CreateShuffleVector( 966c14582f2STobias Grosser ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); 9673b11a16aSHongbin Zheng return VectorLoad; 9683b11a16aSHongbin Zheng } 9693b11a16aSHongbin Zheng 970be9c9117SJohannes Doerfert Value *VectorBlockGenerator::generateUnknownStrideLoad( 9712f1acac6STobias Grosser ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, 97209e3697fSJohannes Doerfert __isl_keep isl_id_to_ast_expr *NewAccesses) { 9733b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 9749646e3feSTobias Grosser auto *Pointer = Load->getPointerOperand(); 9753b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get( 9763b11a16aSHongbin Zheng dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); 9773b11a16aSHongbin Zheng 9783b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 9793b11a16aSHongbin Zheng 9803b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) { 98170131d34SMichael Kruse Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[i], 98270131d34SMichael Kruse VLTS[i], NewAccesses); 983c14582f2STobias Grosser Value *ScalarLoad = 984c14582f2STobias Grosser Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 985c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 986c14582f2STobias Grosser Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); 9873b11a16aSHongbin Zheng } 9883b11a16aSHongbin Zheng 9893b11a16aSHongbin Zheng return Vector; 9903b11a16aSHongbin Zheng } 9913b11a16aSHongbin Zheng 9922d1ed0bfSTobias Grosser void VectorBlockGenerator::generateLoad( 9932f1acac6STobias Grosser ScopStmt &Stmt, LoadInst *Load, ValueMapT &VectorMap, 9942d1ed0bfSTobias Grosser VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 995c1db67e2SJohannes Doerfert if (Value *PreloadLoad = GlobalMap.lookup(Load)) { 996c1db67e2SJohannes Doerfert VectorMap[Load] = Builder.CreateVectorSplat(getVectorWidth(), PreloadLoad, 997c1db67e2SJohannes Doerfert Load->getName() + "_p"); 998c1db67e2SJohannes Doerfert return; 999c1db67e2SJohannes Doerfert } 1000c1db67e2SJohannes Doerfert 10012873645cSTobias Grosser if (!VectorType::isValidElementType(Load->getType())) { 10023b11a16aSHongbin Zheng for (int i = 0; i < getVectorWidth(); i++) 1003bc132607STobias Grosser ScalarMaps[i][Load] = 1004888ab551SMichael Kruse generateArrayLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses); 10053b11a16aSHongbin Zheng return; 10063b11a16aSHongbin Zheng } 10073b11a16aSHongbin Zheng 1008184a4926STobias Grosser const MemoryAccess &Access = Stmt.getArrayAccessFor(Load); 10093b11a16aSHongbin Zheng 101095493984STobias Grosser // Make sure we have scalar values available to access the pointer to 101195493984STobias Grosser // the data location. 101295493984STobias Grosser extractScalarValues(Load, VectorMap, ScalarMaps); 101395493984STobias Grosser 10143b11a16aSHongbin Zheng Value *NewLoad; 1015a00a0291SSebastian Pop if (Access.isStrideZero(isl_map_copy(Schedule))) 10162d1ed0bfSTobias Grosser NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses); 1017a00a0291SSebastian Pop else if (Access.isStrideOne(isl_map_copy(Schedule))) 10182d1ed0bfSTobias Grosser NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses); 10190dd463faSTobias Grosser else if (Access.isStrideX(isl_map_copy(Schedule), -1)) 10202d1ed0bfSTobias Grosser NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true); 10213b11a16aSHongbin Zheng else 10222d1ed0bfSTobias Grosser NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses); 10233b11a16aSHongbin Zheng 10243b11a16aSHongbin Zheng VectorMap[Load] = NewLoad; 10253b11a16aSHongbin Zheng } 10263b11a16aSHongbin Zheng 10272f1acac6STobias Grosser void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, UnaryInstruction *Inst, 10283b11a16aSHongbin Zheng ValueMapT &VectorMap, 10293b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 10303b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 1031be9c9117SJohannes Doerfert Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap, 10322c3ffc04SJohannes Doerfert ScalarMaps, getLoopForStmt(Stmt)); 10333b11a16aSHongbin Zheng 10343b11a16aSHongbin Zheng assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); 10353b11a16aSHongbin Zheng 10363b11a16aSHongbin Zheng const CastInst *Cast = dyn_cast<CastInst>(Inst); 10373b11a16aSHongbin Zheng VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); 10383b11a16aSHongbin Zheng VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); 10393b11a16aSHongbin Zheng } 10403b11a16aSHongbin Zheng 10412f1acac6STobias Grosser void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, BinaryOperator *Inst, 10423b11a16aSHongbin Zheng ValueMapT &VectorMap, 10433b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 10442c3ffc04SJohannes Doerfert Loop *L = getLoopForStmt(Stmt); 10453b11a16aSHongbin Zheng Value *OpZero = Inst->getOperand(0); 10463b11a16aSHongbin Zheng Value *OpOne = Inst->getOperand(1); 10473b11a16aSHongbin Zheng 10483b11a16aSHongbin Zheng Value *NewOpZero, *NewOpOne; 1049be9c9117SJohannes Doerfert NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L); 1050be9c9117SJohannes Doerfert NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L); 10513b11a16aSHongbin Zheng 10521bb59b0dSTobias Grosser Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, 10533b11a16aSHongbin Zheng Inst->getName() + "p_vec"); 10543b11a16aSHongbin Zheng VectorMap[Inst] = NewInst; 10553b11a16aSHongbin Zheng } 10563b11a16aSHongbin Zheng 10572d1ed0bfSTobias Grosser void VectorBlockGenerator::copyStore( 10582f1acac6STobias Grosser ScopStmt &Stmt, StoreInst *Store, ValueMapT &VectorMap, 10592d1ed0bfSTobias Grosser VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 1060184a4926STobias Grosser const MemoryAccess &Access = Stmt.getArrayAccessFor(Store); 10613b11a16aSHongbin Zheng 10629646e3feSTobias Grosser auto *Pointer = Store->getPointerOperand(); 1063be9c9117SJohannes Doerfert Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap, 10642c3ffc04SJohannes Doerfert ScalarMaps, getLoopForStmt(Stmt)); 10653b11a16aSHongbin Zheng 106650fd7010STobias Grosser // Make sure we have scalar values available to access the pointer to 106750fd7010STobias Grosser // the data location. 106850fd7010STobias Grosser extractScalarValues(Store, VectorMap, ScalarMaps); 106950fd7010STobias Grosser 1070a00a0291SSebastian Pop if (Access.isStrideOne(isl_map_copy(Schedule))) { 10711947f863SJohannes Doerfert Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); 107270131d34SMichael Kruse Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[0], 107370131d34SMichael Kruse VLTS[0], NewAccesses); 10743b11a16aSHongbin Zheng 1075c14582f2STobias Grosser Value *VectorPtr = 1076c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 10773b11a16aSHongbin Zheng StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); 10783b11a16aSHongbin Zheng 10793b11a16aSHongbin Zheng if (!Aligned) 10803b11a16aSHongbin Zheng Store->setAlignment(8); 10813b11a16aSHongbin Zheng } else { 10823b11a16aSHongbin Zheng for (unsigned i = 0; i < ScalarMaps.size(); i++) { 10831bb59b0dSTobias Grosser Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); 108470131d34SMichael Kruse Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[i], 108570131d34SMichael Kruse VLTS[i], NewAccesses); 10863b11a16aSHongbin Zheng Builder.CreateStore(Scalar, NewPointer); 10873b11a16aSHongbin Zheng } 10883b11a16aSHongbin Zheng } 10893b11a16aSHongbin Zheng } 10903b11a16aSHongbin Zheng 10913b11a16aSHongbin Zheng bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, 10923b11a16aSHongbin Zheng ValueMapT &VectorMap) { 109391f5b262STobias Grosser for (Value *Operand : Inst->operands()) 109491f5b262STobias Grosser if (VectorMap.count(Operand)) 10953b11a16aSHongbin Zheng return true; 10963b11a16aSHongbin Zheng return false; 10973b11a16aSHongbin Zheng } 10983b11a16aSHongbin Zheng 10993b11a16aSHongbin Zheng bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, 11003b11a16aSHongbin Zheng ValueMapT &VectorMap, 11013b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 11023b11a16aSHongbin Zheng bool HasVectorOperand = false; 11033b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 11043b11a16aSHongbin Zheng 110591f5b262STobias Grosser for (Value *Operand : Inst->operands()) { 110691f5b262STobias Grosser ValueMapT::iterator VecOp = VectorMap.find(Operand); 11073b11a16aSHongbin Zheng 11083b11a16aSHongbin Zheng if (VecOp == VectorMap.end()) 11093b11a16aSHongbin Zheng continue; 11103b11a16aSHongbin Zheng 11113b11a16aSHongbin Zheng HasVectorOperand = true; 11123b11a16aSHongbin Zheng Value *NewVector = VecOp->second; 11133b11a16aSHongbin Zheng 11143b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; ++i) { 11153b11a16aSHongbin Zheng ValueMapT &SM = ScalarMaps[i]; 11163b11a16aSHongbin Zheng 11173b11a16aSHongbin Zheng // If there is one scalar extracted, all scalar elements should have 11183b11a16aSHongbin Zheng // already been extracted by the code here. So no need to check for the 11192219d157STobias Grosser // existence of all of them. 112091f5b262STobias Grosser if (SM.count(Operand)) 11213b11a16aSHongbin Zheng break; 11223b11a16aSHongbin Zheng 112391f5b262STobias Grosser SM[Operand] = 112491f5b262STobias Grosser Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); 11253b11a16aSHongbin Zheng } 11263b11a16aSHongbin Zheng } 11273b11a16aSHongbin Zheng 11283b11a16aSHongbin Zheng return HasVectorOperand; 11293b11a16aSHongbin Zheng } 11303b11a16aSHongbin Zheng 11312d1ed0bfSTobias Grosser void VectorBlockGenerator::copyInstScalarized( 11322f1acac6STobias Grosser ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, 11332d1ed0bfSTobias Grosser VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 11343b11a16aSHongbin Zheng bool HasVectorOperand; 11353b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 11363b11a16aSHongbin Zheng 11373b11a16aSHongbin Zheng HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); 11383b11a16aSHongbin Zheng 11393b11a16aSHongbin Zheng for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) 1140be9c9117SJohannes Doerfert BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane], 1141bc132607STobias Grosser VLTS[VectorLane], NewAccesses); 11423b11a16aSHongbin Zheng 11433b11a16aSHongbin Zheng if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) 11443b11a16aSHongbin Zheng return; 11453b11a16aSHongbin Zheng 11463b11a16aSHongbin Zheng // Make the result available as vector value. 11473b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); 11483b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 11493b11a16aSHongbin Zheng 11503b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) 11513b11a16aSHongbin Zheng Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], 11523b11a16aSHongbin Zheng Builder.getInt32(i)); 11533b11a16aSHongbin Zheng 11543b11a16aSHongbin Zheng VectorMap[Inst] = Vector; 11553b11a16aSHongbin Zheng } 11563b11a16aSHongbin Zheng 1157bc132607STobias Grosser int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); } 11583b11a16aSHongbin Zheng 11592d1ed0bfSTobias Grosser void VectorBlockGenerator::copyInstruction( 11602f1acac6STobias Grosser ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, 11612d1ed0bfSTobias Grosser VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { 11623b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 11633b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 11643b11a16aSHongbin Zheng if (Inst->isTerminator()) 11653b11a16aSHongbin Zheng return; 11663b11a16aSHongbin Zheng 11675dced269SJohannes Doerfert if (canSyntheziseInStmt(Stmt, Inst)) 1168e71c6ab5STobias Grosser return; 1169e71c6ab5STobias Grosser 11709646e3feSTobias Grosser if (auto *Load = dyn_cast<LoadInst>(Inst)) { 11712d1ed0bfSTobias Grosser generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses); 11723b11a16aSHongbin Zheng return; 11733b11a16aSHongbin Zheng } 11743b11a16aSHongbin Zheng 11753b11a16aSHongbin Zheng if (hasVectorOperands(Inst, VectorMap)) { 11769646e3feSTobias Grosser if (auto *Store = dyn_cast<StoreInst>(Inst)) { 11770446d81eSMichael Kruse // Identified as redundant by -polly-simplify. 11780446d81eSMichael Kruse if (!Stmt.getArrayAccessOrNULLFor(Store)) 11790446d81eSMichael Kruse return; 11800446d81eSMichael Kruse 11812d1ed0bfSTobias Grosser copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses); 11823b11a16aSHongbin Zheng return; 11833b11a16aSHongbin Zheng } 11843b11a16aSHongbin Zheng 11859646e3feSTobias Grosser if (auto *Unary = dyn_cast<UnaryInstruction>(Inst)) { 1186be9c9117SJohannes Doerfert copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps); 11873b11a16aSHongbin Zheng return; 11883b11a16aSHongbin Zheng } 11893b11a16aSHongbin Zheng 11909646e3feSTobias Grosser if (auto *Binary = dyn_cast<BinaryOperator>(Inst)) { 1191be9c9117SJohannes Doerfert copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps); 11923b11a16aSHongbin Zheng return; 11933b11a16aSHongbin Zheng } 11943b11a16aSHongbin Zheng 11953b11a16aSHongbin Zheng // Falltrough: We generate scalar instructions, if we don't know how to 11963b11a16aSHongbin Zheng // generate vector code. 11973b11a16aSHongbin Zheng } 11983b11a16aSHongbin Zheng 11992d1ed0bfSTobias Grosser copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses); 12003b11a16aSHongbin Zheng } 12013b11a16aSHongbin Zheng 1202a69d4f0dSTobias Grosser void VectorBlockGenerator::generateScalarVectorLoads( 1203a69d4f0dSTobias Grosser ScopStmt &Stmt, ValueMapT &VectorBlockMap) { 1204a69d4f0dSTobias Grosser for (MemoryAccess *MA : Stmt) { 1205a69d4f0dSTobias Grosser if (MA->isArrayKind() || MA->isWrite()) 1206a69d4f0dSTobias Grosser continue; 1207a69d4f0dSTobias Grosser 1208a69d4f0dSTobias Grosser auto *Address = getOrCreateAlloca(*MA); 1209a69d4f0dSTobias Grosser Type *VectorPtrType = getVectorPtrTy(Address, 1); 1210a69d4f0dSTobias Grosser Value *VectorPtr = Builder.CreateBitCast(Address, VectorPtrType, 1211a69d4f0dSTobias Grosser Address->getName() + "_p_vec_p"); 1212a69d4f0dSTobias Grosser auto *Val = Builder.CreateLoad(VectorPtr, Address->getName() + ".reload"); 1213a69d4f0dSTobias Grosser Constant *SplatVector = Constant::getNullValue( 1214a69d4f0dSTobias Grosser VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 1215a69d4f0dSTobias Grosser 1216a69d4f0dSTobias Grosser Value *VectorVal = Builder.CreateShuffleVector( 1217a69d4f0dSTobias Grosser Val, Val, SplatVector, Address->getName() + "_p_splat"); 1218583be06fSTobias Grosser VectorBlockMap[MA->getAccessValue()] = VectorVal; 1219a69d4f0dSTobias Grosser } 1220a69d4f0dSTobias Grosser } 1221a69d4f0dSTobias Grosser 1222a69d4f0dSTobias Grosser void VectorBlockGenerator::verifyNoScalarStores(ScopStmt &Stmt) { 1223a69d4f0dSTobias Grosser for (MemoryAccess *MA : Stmt) { 1224a69d4f0dSTobias Grosser if (MA->isArrayKind() || MA->isRead()) 1225a69d4f0dSTobias Grosser continue; 1226a69d4f0dSTobias Grosser 1227a69d4f0dSTobias Grosser llvm_unreachable("Scalar stores not expected in vector loop"); 1228a69d4f0dSTobias Grosser } 1229a69d4f0dSTobias Grosser } 1230a69d4f0dSTobias Grosser 12312d1ed0bfSTobias Grosser void VectorBlockGenerator::copyStmt( 12322d1ed0bfSTobias Grosser ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) { 123321a059afSTobias Grosser assert(Stmt.isBlockStmt() && 123421a059afSTobias Grosser "TODO: Only block statements can be copied by the vector block " 123521a059afSTobias Grosser "generator"); 1236275a1756SJohannes Doerfert 1237be9c9117SJohannes Doerfert BasicBlock *BB = Stmt.getBasicBlock(); 1238b8f58b53SDuncan P. N. Exon Smith BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), 1239b8f58b53SDuncan P. N. Exon Smith &*Builder.GetInsertPoint(), &DT, &LI); 12403b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 1241b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&CopyBB->front()); 12423b11a16aSHongbin Zheng 12433b11a16aSHongbin Zheng // Create two maps that store the mapping from the original instructions of 12443b11a16aSHongbin Zheng // the old basic block to their copies in the new basic block. Those maps 12453b11a16aSHongbin Zheng // are basic block local. 12463b11a16aSHongbin Zheng // 12473b11a16aSHongbin Zheng // As vector code generation is supported there is one map for scalar values 12483b11a16aSHongbin Zheng // and one for vector values. 12493b11a16aSHongbin Zheng // 12503b11a16aSHongbin Zheng // In case we just do scalar code generation, the vectorMap is not used and 12513b11a16aSHongbin Zheng // the scalarMap has just one dimension, which contains the mapping. 12523b11a16aSHongbin Zheng // 12533b11a16aSHongbin Zheng // In case vector code generation is done, an instruction may either appear 12543b11a16aSHongbin Zheng // in the vector map once (as it is calculating >vectorwidth< values at a 12553b11a16aSHongbin Zheng // time. Or (if the values are calculated using scalar operations), it 12563b11a16aSHongbin Zheng // appears once in every dimension of the scalarMap. 12573b11a16aSHongbin Zheng VectorValueMapT ScalarBlockMap(getVectorWidth()); 12583b11a16aSHongbin Zheng ValueMapT VectorBlockMap; 12593b11a16aSHongbin Zheng 1260a69d4f0dSTobias Grosser generateScalarVectorLoads(Stmt, VectorBlockMap); 1261a69d4f0dSTobias Grosser 126291f5b262STobias Grosser for (Instruction &Inst : *BB) 12632d1ed0bfSTobias Grosser copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses); 1264a69d4f0dSTobias Grosser 1265a69d4f0dSTobias Grosser verifyNoScalarStores(Stmt); 12663b11a16aSHongbin Zheng } 1267275a1756SJohannes Doerfert 1268ecff11dcSJohannes Doerfert BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB, 1269ecff11dcSJohannes Doerfert BasicBlock *BBCopy) { 1270514f6efaSJohannes Doerfert 1271514f6efaSJohannes Doerfert BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); 1272514f6efaSJohannes Doerfert BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); 1273514f6efaSJohannes Doerfert 1274514f6efaSJohannes Doerfert if (BBCopyIDom) 1275514f6efaSJohannes Doerfert DT.changeImmediateDominator(BBCopy, BBCopyIDom); 1276514f6efaSJohannes Doerfert 1277514f6efaSJohannes Doerfert return BBCopyIDom; 1278514f6efaSJohannes Doerfert } 1279514f6efaSJohannes Doerfert 1280f33c125dSMichael Kruse // This is to determine whether an llvm::Value (defined in @p BB) is usable when 1281f33c125dSMichael Kruse // leaving a subregion. The straight-forward DT.dominates(BB, R->getExitBlock()) 1282f33c125dSMichael Kruse // does not work in cases where the exit block has edges from outside the 1283f33c125dSMichael Kruse // region. In that case the llvm::Value would never be usable in in the exit 1284f33c125dSMichael Kruse // block. The RegionGenerator however creates an new exit block ('ExitBBCopy') 1285f33c125dSMichael Kruse // for the subregion's exiting edges only. We need to determine whether an 1286f33c125dSMichael Kruse // llvm::Value is usable in there. We do this by checking whether it dominates 1287f33c125dSMichael Kruse // all exiting blocks individually. 1288f33c125dSMichael Kruse static bool isDominatingSubregionExit(const DominatorTree &DT, Region *R, 1289f33c125dSMichael Kruse BasicBlock *BB) { 1290f33c125dSMichael Kruse for (auto ExitingBB : predecessors(R->getExit())) { 1291f33c125dSMichael Kruse // Check for non-subregion incoming edges. 1292f33c125dSMichael Kruse if (!R->contains(ExitingBB)) 1293f33c125dSMichael Kruse continue; 1294f33c125dSMichael Kruse 1295f33c125dSMichael Kruse if (!DT.dominates(BB, ExitingBB)) 1296f33c125dSMichael Kruse return false; 1297f33c125dSMichael Kruse } 1298f33c125dSMichael Kruse 1299f33c125dSMichael Kruse return true; 1300f33c125dSMichael Kruse } 1301f33c125dSMichael Kruse 1302f33c125dSMichael Kruse // Find the direct dominator of the subregion's exit block if the subregion was 1303f33c125dSMichael Kruse // simplified. 1304f33c125dSMichael Kruse static BasicBlock *findExitDominator(DominatorTree &DT, Region *R) { 1305f33c125dSMichael Kruse BasicBlock *Common = nullptr; 1306f33c125dSMichael Kruse for (auto ExitingBB : predecessors(R->getExit())) { 1307f33c125dSMichael Kruse // Check for non-subregion incoming edges. 1308f33c125dSMichael Kruse if (!R->contains(ExitingBB)) 1309f33c125dSMichael Kruse continue; 1310f33c125dSMichael Kruse 1311f33c125dSMichael Kruse // First exiting edge. 1312f33c125dSMichael Kruse if (!Common) { 1313f33c125dSMichael Kruse Common = ExitingBB; 1314f33c125dSMichael Kruse continue; 1315f33c125dSMichael Kruse } 1316f33c125dSMichael Kruse 1317f33c125dSMichael Kruse Common = DT.findNearestCommonDominator(Common, ExitingBB); 1318f33c125dSMichael Kruse } 1319f33c125dSMichael Kruse 1320f33c125dSMichael Kruse assert(Common && R->contains(Common)); 1321f33c125dSMichael Kruse return Common; 1322f33c125dSMichael Kruse } 1323f33c125dSMichael Kruse 1324bc132607STobias Grosser void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, 13252d1ed0bfSTobias Grosser isl_id_to_ast_expr *IdToAstExp) { 1326275a1756SJohannes Doerfert assert(Stmt.isRegionStmt() && 1327d3f21833STobias Grosser "Only region statements can be copied by the region generator"); 1328275a1756SJohannes Doerfert 1329ecff11dcSJohannes Doerfert // Forget all old mappings. 1330ecff11dcSJohannes Doerfert BlockMap.clear(); 1331ecff11dcSJohannes Doerfert RegionMaps.clear(); 1332ecff11dcSJohannes Doerfert IncompletePHINodeMap.clear(); 1333ecff11dcSJohannes Doerfert 1334225f0d1eSMichael Kruse // Collection of all values related to this subregion. 1335225f0d1eSMichael Kruse ValueMapT ValueMap; 1336225f0d1eSMichael Kruse 1337275a1756SJohannes Doerfert // The region represented by the statement. 1338275a1756SJohannes Doerfert Region *R = Stmt.getRegion(); 1339275a1756SJohannes Doerfert 1340ecff11dcSJohannes Doerfert // Create a dedicated entry for the region where we can reload all demoted 1341ecff11dcSJohannes Doerfert // inputs. 1342ecff11dcSJohannes Doerfert BasicBlock *EntryBB = R->getEntry(); 1343b8f58b53SDuncan P. N. Exon Smith BasicBlock *EntryBBCopy = SplitBlock(Builder.GetInsertBlock(), 1344b8f58b53SDuncan P. N. Exon Smith &*Builder.GetInsertPoint(), &DT, &LI); 1345ecff11dcSJohannes Doerfert EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry"); 1346b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&EntryBBCopy->front()); 1347514f6efaSJohannes Doerfert 1348c993739eSMichael Kruse ValueMapT &EntryBBMap = RegionMaps[EntryBBCopy]; 13492fa35194SMichael Kruse generateScalarLoads(Stmt, LTS, EntryBBMap, IdToAstExp); 1350225f0d1eSMichael Kruse 1351ecff11dcSJohannes Doerfert for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI) 1352ecff11dcSJohannes Doerfert if (!R->contains(*PI)) 1353ecff11dcSJohannes Doerfert BlockMap[*PI] = EntryBBCopy; 1354275a1756SJohannes Doerfert 1355275a1756SJohannes Doerfert // Iterate over all blocks in the region in a breadth-first search. 1356275a1756SJohannes Doerfert std::deque<BasicBlock *> Blocks; 13575b1abfc8SMandeep Singh Grang SmallSetVector<BasicBlock *, 8> SeenBlocks; 1358ecff11dcSJohannes Doerfert Blocks.push_back(EntryBB); 1359ecff11dcSJohannes Doerfert SeenBlocks.insert(EntryBB); 1360275a1756SJohannes Doerfert 1361275a1756SJohannes Doerfert while (!Blocks.empty()) { 1362275a1756SJohannes Doerfert BasicBlock *BB = Blocks.front(); 1363275a1756SJohannes Doerfert Blocks.pop_front(); 1364275a1756SJohannes Doerfert 1365514f6efaSJohannes Doerfert // First split the block and update dominance information. 1366514f6efaSJohannes Doerfert BasicBlock *BBCopy = splitBB(BB); 1367ecff11dcSJohannes Doerfert BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy); 1368ecff11dcSJohannes Doerfert 1369c993739eSMichael Kruse // Get the mapping for this block and initialize it with either the scalar 1370c993739eSMichael Kruse // loads from the generated entering block (which dominates all blocks of 1371c993739eSMichael Kruse // this subregion) or the maps of the immediate dominator, if part of the 1372c993739eSMichael Kruse // subregion. The latter necessarily includes the former. 1373c993739eSMichael Kruse ValueMapT *InitBBMap; 1374c993739eSMichael Kruse if (BBCopyIDom) { 1375c993739eSMichael Kruse assert(RegionMaps.count(BBCopyIDom)); 1376c993739eSMichael Kruse InitBBMap = &RegionMaps[BBCopyIDom]; 1377c993739eSMichael Kruse } else 1378c993739eSMichael Kruse InitBBMap = &EntryBBMap; 1379c993739eSMichael Kruse auto Inserted = RegionMaps.insert(std::make_pair(BBCopy, *InitBBMap)); 1380c993739eSMichael Kruse ValueMapT &RegionMap = Inserted.first->second; 1381514f6efaSJohannes Doerfert 1382275a1756SJohannes Doerfert // Copy the block with the BlockGenerator. 1383b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&BBCopy->front()); 1384bc132607STobias Grosser copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp); 1385275a1756SJohannes Doerfert 1386ecff11dcSJohannes Doerfert // In order to remap PHI nodes we store also basic block mappings. 1387ecff11dcSJohannes Doerfert BlockMap[BB] = BBCopy; 1388ecff11dcSJohannes Doerfert 1389ecff11dcSJohannes Doerfert // Add values to incomplete PHI nodes waiting for this block to be copied. 1390ecff11dcSJohannes Doerfert for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB]) 1391bc132607STobias Grosser addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS); 1392ecff11dcSJohannes Doerfert IncompletePHINodeMap[BB].clear(); 1393ecff11dcSJohannes Doerfert 1394275a1756SJohannes Doerfert // And continue with new successors inside the region. 1395275a1756SJohannes Doerfert for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++) 13965b1abfc8SMandeep Singh Grang if (R->contains(*SI) && SeenBlocks.insert(*SI)) 1397275a1756SJohannes Doerfert Blocks.push_back(*SI); 1398ebffcbeeSMichael Kruse 1399ebffcbeeSMichael Kruse // Remember value in case it is visible after this subregion. 1400f33c125dSMichael Kruse if (isDominatingSubregionExit(DT, R, BB)) 1401ebffcbeeSMichael Kruse ValueMap.insert(RegionMap.begin(), RegionMap.end()); 1402275a1756SJohannes Doerfert } 1403275a1756SJohannes Doerfert 1404275a1756SJohannes Doerfert // Now create a new dedicated region exit block and add it to the region map. 1405b8f58b53SDuncan P. N. Exon Smith BasicBlock *ExitBBCopy = SplitBlock(Builder.GetInsertBlock(), 1406b8f58b53SDuncan P. N. Exon Smith &*Builder.GetInsertPoint(), &DT, &LI); 1407ecff11dcSJohannes Doerfert ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit"); 1408514f6efaSJohannes Doerfert BlockMap[R->getExit()] = ExitBBCopy; 1409514f6efaSJohannes Doerfert 1410f33c125dSMichael Kruse BasicBlock *ExitDomBBCopy = BlockMap.lookup(findExitDominator(DT, R)); 141121a059afSTobias Grosser assert(ExitDomBBCopy && 141221a059afSTobias Grosser "Common exit dominator must be within region; at least the entry node " 141321a059afSTobias Grosser "must match"); 1414f33c125dSMichael Kruse DT.changeImmediateDominator(ExitBBCopy, ExitDomBBCopy); 1415275a1756SJohannes Doerfert 1416275a1756SJohannes Doerfert // As the block generator doesn't handle control flow we need to add the 1417275a1756SJohannes Doerfert // region control flow by hand after all blocks have been copied. 1418275a1756SJohannes Doerfert for (BasicBlock *BB : SeenBlocks) { 1419275a1756SJohannes Doerfert 1420514f6efaSJohannes Doerfert BasicBlock *BBCopy = BlockMap[BB]; 1421e114dc02SJohannes Doerfert TerminatorInst *TI = BB->getTerminator(); 1422e114dc02SJohannes Doerfert if (isa<UnreachableInst>(TI)) { 1423e114dc02SJohannes Doerfert while (!BBCopy->empty()) 1424e114dc02SJohannes Doerfert BBCopy->begin()->eraseFromParent(); 1425e114dc02SJohannes Doerfert new UnreachableInst(BBCopy->getContext(), BBCopy); 1426e114dc02SJohannes Doerfert continue; 1427e114dc02SJohannes Doerfert } 1428e114dc02SJohannes Doerfert 1429275a1756SJohannes Doerfert Instruction *BICopy = BBCopy->getTerminator(); 1430275a1756SJohannes Doerfert 1431514f6efaSJohannes Doerfert ValueMapT &RegionMap = RegionMaps[BBCopy]; 1432514f6efaSJohannes Doerfert RegionMap.insert(BlockMap.begin(), BlockMap.end()); 1433514f6efaSJohannes Doerfert 143445e7944bSTobias Grosser Builder.SetInsertPoint(BICopy); 14359a132f36SJohannes Doerfert copyInstScalar(Stmt, TI, RegionMap, LTS); 1436275a1756SJohannes Doerfert BICopy->eraseFromParent(); 1437275a1756SJohannes Doerfert } 1438275a1756SJohannes Doerfert 1439ecff11dcSJohannes Doerfert // Add counting PHI nodes to all loops in the region that can be used as 1440ecff11dcSJohannes Doerfert // replacement for SCEVs refering to the old loop. 1441ecff11dcSJohannes Doerfert for (BasicBlock *BB : SeenBlocks) { 1442ecff11dcSJohannes Doerfert Loop *L = LI.getLoopFor(BB); 1443bc29e0b2STobias Grosser if (L == nullptr || L->getHeader() != BB || !R->contains(L)) 1444ecff11dcSJohannes Doerfert continue; 1445ecff11dcSJohannes Doerfert 1446ecff11dcSJohannes Doerfert BasicBlock *BBCopy = BlockMap[BB]; 1447ecff11dcSJohannes Doerfert Value *NullVal = Builder.getInt32(0); 1448ecff11dcSJohannes Doerfert PHINode *LoopPHI = 1449ecff11dcSJohannes Doerfert PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv"); 1450ecff11dcSJohannes Doerfert Instruction *LoopPHIInc = BinaryOperator::CreateAdd( 1451ecff11dcSJohannes Doerfert LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc"); 1452b8f58b53SDuncan P. N. Exon Smith LoopPHI->insertBefore(&BBCopy->front()); 1453ecff11dcSJohannes Doerfert LoopPHIInc->insertBefore(BBCopy->getTerminator()); 1454ecff11dcSJohannes Doerfert 1455ecff11dcSJohannes Doerfert for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) { 1456ecff11dcSJohannes Doerfert if (!R->contains(PredBB)) 1457ecff11dcSJohannes Doerfert continue; 1458ecff11dcSJohannes Doerfert if (L->contains(PredBB)) 1459ecff11dcSJohannes Doerfert LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]); 1460ecff11dcSJohannes Doerfert else 1461ecff11dcSJohannes Doerfert LoopPHI->addIncoming(NullVal, BlockMap[PredBB]); 1462ecff11dcSJohannes Doerfert } 1463ecff11dcSJohannes Doerfert 1464ecff11dcSJohannes Doerfert for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy))) 1465ecff11dcSJohannes Doerfert if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0) 1466ecff11dcSJohannes Doerfert LoopPHI->addIncoming(NullVal, PredBBCopy); 1467ecff11dcSJohannes Doerfert 1468ecff11dcSJohannes Doerfert LTS[L] = SE.getUnknown(LoopPHI); 1469ecff11dcSJohannes Doerfert } 1470ecff11dcSJohannes Doerfert 1471225f0d1eSMichael Kruse // Continue generating code in the exit block. 1472b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&*ExitBBCopy->getFirstInsertionPt()); 1473225f0d1eSMichael Kruse 1474225f0d1eSMichael Kruse // Write values visible to other statements. 14752fa35194SMichael Kruse generateScalarStores(Stmt, LTS, ValueMap, IdToAstExp); 14763e956020STobias Grosser BlockMap.clear(); 14773e956020STobias Grosser RegionMaps.clear(); 14783e956020STobias Grosser IncompletePHINodeMap.clear(); 1479275a1756SJohannes Doerfert } 1480ecff11dcSJohannes Doerfert 1481ee6a4fc6SMichael Kruse PHINode *RegionGenerator::buildExitPHI(MemoryAccess *MA, LoopToScevMapT <S, 1482ee6a4fc6SMichael Kruse ValueMapT &BBMap, Loop *L) { 1483ee6a4fc6SMichael Kruse ScopStmt *Stmt = MA->getStatement(); 1484ee6a4fc6SMichael Kruse Region *SubR = Stmt->getRegion(); 1485ee6a4fc6SMichael Kruse auto Incoming = MA->getIncoming(); 1486ee6a4fc6SMichael Kruse 1487ee6a4fc6SMichael Kruse PollyIRBuilder::InsertPointGuard IPGuard(Builder); 1488ee6a4fc6SMichael Kruse PHINode *OrigPHI = cast<PHINode>(MA->getAccessInstruction()); 1489ee6a4fc6SMichael Kruse BasicBlock *NewSubregionExit = Builder.GetInsertBlock(); 1490ee6a4fc6SMichael Kruse 1491ee6a4fc6SMichael Kruse // This can happen if the subregion is simplified after the ScopStmts 1492ee6a4fc6SMichael Kruse // have been created; simplification happens as part of CodeGeneration. 1493ee6a4fc6SMichael Kruse if (OrigPHI->getParent() != SubR->getExit()) { 1494ee6a4fc6SMichael Kruse BasicBlock *FormerExit = SubR->getExitingBlock(); 1495ee6a4fc6SMichael Kruse if (FormerExit) 1496ee6a4fc6SMichael Kruse NewSubregionExit = BlockMap.lookup(FormerExit); 1497ee6a4fc6SMichael Kruse } 1498ee6a4fc6SMichael Kruse 1499ee6a4fc6SMichael Kruse PHINode *NewPHI = PHINode::Create(OrigPHI->getType(), Incoming.size(), 1500ee6a4fc6SMichael Kruse "polly." + OrigPHI->getName(), 1501ee6a4fc6SMichael Kruse NewSubregionExit->getFirstNonPHI()); 1502ee6a4fc6SMichael Kruse 1503ee6a4fc6SMichael Kruse // Add the incoming values to the PHI. 1504ee6a4fc6SMichael Kruse for (auto &Pair : Incoming) { 1505ee6a4fc6SMichael Kruse BasicBlock *OrigIncomingBlock = Pair.first; 1506ee6a4fc6SMichael Kruse BasicBlock *NewIncomingBlock = BlockMap.lookup(OrigIncomingBlock); 1507ee6a4fc6SMichael Kruse Builder.SetInsertPoint(NewIncomingBlock->getTerminator()); 1508ee6a4fc6SMichael Kruse assert(RegionMaps.count(NewIncomingBlock)); 1509ee6a4fc6SMichael Kruse ValueMapT *LocalBBMap = &RegionMaps[NewIncomingBlock]; 1510ee6a4fc6SMichael Kruse 1511ee6a4fc6SMichael Kruse Value *OrigIncomingValue = Pair.second; 1512ee6a4fc6SMichael Kruse Value *NewIncomingValue = 1513ee6a4fc6SMichael Kruse getNewValue(*Stmt, OrigIncomingValue, *LocalBBMap, LTS, L); 1514ee6a4fc6SMichael Kruse NewPHI->addIncoming(NewIncomingValue, NewIncomingBlock); 1515ee6a4fc6SMichael Kruse } 1516ee6a4fc6SMichael Kruse 1517ee6a4fc6SMichael Kruse return NewPHI; 1518ee6a4fc6SMichael Kruse } 1519ee6a4fc6SMichael Kruse 1520ee6a4fc6SMichael Kruse Value *RegionGenerator::getExitScalar(MemoryAccess *MA, LoopToScevMapT <S, 1521ee6a4fc6SMichael Kruse ValueMapT &BBMap) { 1522ee6a4fc6SMichael Kruse ScopStmt *Stmt = MA->getStatement(); 1523ee6a4fc6SMichael Kruse 1524ee6a4fc6SMichael Kruse // TODO: Add some test cases that ensure this is really the right choice. 1525ee6a4fc6SMichael Kruse Loop *L = LI.getLoopFor(Stmt->getRegion()->getExit()); 1526ee6a4fc6SMichael Kruse 1527ee6a4fc6SMichael Kruse if (MA->isAnyPHIKind()) { 1528ee6a4fc6SMichael Kruse auto Incoming = MA->getIncoming(); 1529ee6a4fc6SMichael Kruse assert(!Incoming.empty() && 1530ee6a4fc6SMichael Kruse "PHI WRITEs must have originate from at least one incoming block"); 1531ee6a4fc6SMichael Kruse 1532ee6a4fc6SMichael Kruse // If there is only one incoming value, we do not need to create a PHI. 1533ee6a4fc6SMichael Kruse if (Incoming.size() == 1) { 1534ee6a4fc6SMichael Kruse Value *OldVal = Incoming[0].second; 1535ee6a4fc6SMichael Kruse return getNewValue(*Stmt, OldVal, BBMap, LTS, L); 1536ee6a4fc6SMichael Kruse } 1537ee6a4fc6SMichael Kruse 1538ee6a4fc6SMichael Kruse return buildExitPHI(MA, LTS, BBMap, L); 1539ee6a4fc6SMichael Kruse } 1540ee6a4fc6SMichael Kruse 15414d5a9172STobias Grosser // MemoryKind::Value accesses leaving the subregion must dominate the exit 15424d5a9172STobias Grosser // block; just pass the copied value. 1543ee6a4fc6SMichael Kruse Value *OldVal = MA->getAccessValue(); 1544ee6a4fc6SMichael Kruse return getNewValue(*Stmt, OldVal, BBMap, LTS, L); 1545ee6a4fc6SMichael Kruse } 1546ee6a4fc6SMichael Kruse 15472fa35194SMichael Kruse void RegionGenerator::generateScalarStores( 15482fa35194SMichael Kruse ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, 15492fa35194SMichael Kruse __isl_keep isl_id_to_ast_expr *NewAccesses) { 155075296901STobias Grosser assert(Stmt.getRegion() && 155175296901STobias Grosser "Block statements need to use the generateScalarStores() " 1552ecff11dcSJohannes Doerfert "function in the BlockGenerator"); 1553ecff11dcSJohannes Doerfert 1554ecff11dcSJohannes Doerfert for (MemoryAccess *MA : Stmt) { 15552fa35194SMichael Kruse if (MA->isOriginalArrayKind() || MA->isRead()) 1556ecff11dcSJohannes Doerfert continue; 1557ecff11dcSJohannes Doerfert 1558706f79abSMichael Kruse isl::set AccDom = give(isl_map_domain(MA->getAccessRelation())); 1559706f79abSMichael Kruse const char *Subject = isl_id_get_name(give(MA->getId()).keep()); 1560706f79abSMichael Kruse generateConditionalExecution(Stmt, AccDom, Subject, [&, this, MA]() { 1561706f79abSMichael Kruse 1562ee6a4fc6SMichael Kruse Value *NewVal = getExitScalar(MA, LTS, BBMap); 1563706f79abSMichael Kruse Value *Address = getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, 1564706f79abSMichael Kruse NewAccesses); 1565eac9726eSMichael Kruse assert((!isa<Instruction>(NewVal) || 1566eac9726eSMichael Kruse DT.dominates(cast<Instruction>(NewVal)->getParent(), 1567eac9726eSMichael Kruse Builder.GetInsertBlock())) && 1568eac9726eSMichael Kruse "Domination violation"); 1569eac9726eSMichael Kruse assert((!isa<Instruction>(Address) || 1570eac9726eSMichael Kruse DT.dominates(cast<Instruction>(Address)->getParent(), 1571eac9726eSMichael Kruse Builder.GetInsertBlock())) && 1572eac9726eSMichael Kruse "Domination violation"); 1573ee6a4fc6SMichael Kruse Builder.CreateStore(NewVal, Address); 1574706f79abSMichael Kruse }); 1575ecff11dcSJohannes Doerfert } 1576ecff11dcSJohannes Doerfert } 1577ecff11dcSJohannes Doerfert 1578943c369cSTobias Grosser void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, PHINode *PHI, 1579ecff11dcSJohannes Doerfert PHINode *PHICopy, BasicBlock *IncomingBB, 1580ecff11dcSJohannes Doerfert LoopToScevMapT <S) { 1581ecff11dcSJohannes Doerfert // If the incoming block was not yet copied mark this PHI as incomplete. 1582ecff11dcSJohannes Doerfert // Once the block will be copied the incoming value will be added. 1583ecff11dcSJohannes Doerfert BasicBlock *BBCopy = BlockMap[IncomingBB]; 1584ecff11dcSJohannes Doerfert if (!BBCopy) { 1585c3e9c144SMichael Kruse assert(Stmt.contains(IncomingBB) && 1586ecff11dcSJohannes Doerfert "Bad incoming block for PHI in non-affine region"); 1587ecff11dcSJohannes Doerfert IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy)); 1588ecff11dcSJohannes Doerfert return; 1589ecff11dcSJohannes Doerfert } 1590ecff11dcSJohannes Doerfert 159175dfaa1dSTobias Grosser assert(RegionMaps.count(BBCopy) && "Incoming PHI block did not have a BBMap"); 1592ecff11dcSJohannes Doerfert ValueMapT &BBCopyMap = RegionMaps[BBCopy]; 1593ecff11dcSJohannes Doerfert 159475dfaa1dSTobias Grosser Value *OpCopy = nullptr; 159575dfaa1dSTobias Grosser 1596c3e9c144SMichael Kruse if (Stmt.contains(IncomingBB)) { 1597ecff11dcSJohannes Doerfert Value *Op = PHI->getIncomingValueForBlock(IncomingBB); 1598dc122222SMichael Kruse 15996ba92714SJohannes Doerfert // If the current insert block is different from the PHIs incoming block 16006ba92714SJohannes Doerfert // change it, otherwise do not. 16016ba92714SJohannes Doerfert auto IP = Builder.GetInsertPoint(); 16026ba92714SJohannes Doerfert if (IP->getParent() != BBCopy) 1603dc122222SMichael Kruse Builder.SetInsertPoint(BBCopy->getTerminator()); 16042c3ffc04SJohannes Doerfert OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForStmt(Stmt)); 16056ba92714SJohannes Doerfert if (IP->getParent() != BBCopy) 16066ba92714SJohannes Doerfert Builder.SetInsertPoint(&*IP); 1607ecff11dcSJohannes Doerfert } else { 160875dfaa1dSTobias Grosser // All edges from outside the non-affine region become a single edge 160975dfaa1dSTobias Grosser // in the new copy of the non-affine region. Make sure to only add the 161075dfaa1dSTobias Grosser // corresponding edge the first time we encounter a basic block from 161175dfaa1dSTobias Grosser // outside the non-affine region. 1612ecff11dcSJohannes Doerfert if (PHICopy->getBasicBlockIndex(BBCopy) >= 0) 1613ecff11dcSJohannes Doerfert return; 1614ecff11dcSJohannes Doerfert 161575dfaa1dSTobias Grosser // Get the reloaded value. 161675dfaa1dSTobias Grosser OpCopy = getNewValue(Stmt, PHI, BBCopyMap, LTS, getLoopForStmt(Stmt)); 1617ecff11dcSJohannes Doerfert } 1618ecff11dcSJohannes Doerfert 1619ecff11dcSJohannes Doerfert assert(OpCopy && "Incoming PHI value was not copied properly"); 1620ecff11dcSJohannes Doerfert assert(BBCopy && "Incoming PHI block was not copied properly"); 1621ecff11dcSJohannes Doerfert PHICopy->addIncoming(OpCopy, BBCopy); 1622ecff11dcSJohannes Doerfert } 1623ecff11dcSJohannes Doerfert 16242b809d13STobias Grosser void RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, PHINode *PHI, 1625ecff11dcSJohannes Doerfert ValueMapT &BBMap, 1626ecff11dcSJohannes Doerfert LoopToScevMapT <S) { 1627ecff11dcSJohannes Doerfert unsigned NumIncoming = PHI->getNumIncomingValues(); 1628ecff11dcSJohannes Doerfert PHINode *PHICopy = 1629ecff11dcSJohannes Doerfert Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName()); 1630ecff11dcSJohannes Doerfert PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI()); 1631ecff11dcSJohannes Doerfert BBMap[PHI] = PHICopy; 1632ecff11dcSJohannes Doerfert 163397b84909STobias Grosser for (BasicBlock *IncomingBB : PHI->blocks()) 163497b84909STobias Grosser addOperandToPHI(Stmt, PHI, PHICopy, IncomingBB, LTS); 1635ecff11dcSJohannes Doerfert } 1636