Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1 | //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the BlockGenerator and VectorBlockGenerator classes, |
| 11 | // which generate sequential code and vectorized code for a polyhedral |
| 12 | // statement, respectively. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "polly/ScopInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 17 | #include "isl/aff.h" |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 18 | #include "isl/ast.h" |
| 19 | #include "isl/ast_build.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 20 | #include "isl/set.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 21 | #include "polly/CodeGen/BlockGenerators.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/CodeGeneration.h" |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/IslExprBuilder.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 24 | #include "polly/Options.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 25 | #include "polly/Support/GICHelper.h" |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 26 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 27 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/LoopInfo.h" |
| 29 | #include "llvm/Analysis/ScalarEvolution.h" |
| 30 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IntrinsicInst.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 33 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | using namespace polly; |
| 36 | |
| 37 | static cl::opt<bool> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 38 | Aligned("enable-polly-aligned", |
| 39 | cl::desc("Assumed aligned memory accesses."), cl::Hidden, |
| 40 | cl::value_desc("OpenMP code generation enabled if true"), |
| 41 | cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 42 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 43 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 44 | SCEVCodegenF("polly-codegen-scev", |
| 45 | cl::desc("Use SCEV based code generation."), cl::Hidden, |
| 46 | cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore, |
| 47 | cl::cat(PollyCategory)); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 48 | |
| 49 | bool polly::SCEVCodegen; |
| 50 | |
| 51 | bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI, |
| 52 | ScalarEvolution *SE, const Region *R) { |
| 53 | if (SCEVCodegen) { |
| 54 | if (!I || !SE->isSCEVable(I->getType())) |
| 55 | return false; |
| 56 | |
| 57 | if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I))) |
| 58 | if (!isa<SCEVCouldNotCompute>(Scev)) |
| 59 | if (!hasScalarDepsInsideRegion(Scev, R)) |
| 60 | return true; |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | Loop *L = LI->getLoopFor(I->getParent()); |
Tobias Grosser | e8df5bd | 2013-04-17 07:20:36 +0000 | [diff] [blame] | 66 | return L && I == L->getCanonicalInductionVariable() && R->contains(L); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 69 | BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P, |
| 70 | isl_ast_build *Build, |
| 71 | IslExprBuilder *ExprBuilder) |
| 72 | : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()), |
| 73 | Build(Build), ExprBuilder(ExprBuilder) {} |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 74 | |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 75 | Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap, |
| 76 | ValueMapT &GlobalMap) const { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 77 | // We assume constants never change. |
| 78 | // This avoids map lookups for many calls to this function. |
| 79 | if (isa<Constant>(Old)) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 80 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 81 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 82 | if (Value *New = GlobalMap.lookup(Old)) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 83 | if (Old->getType()->getScalarSizeInBits() < |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 84 | New->getType()->getScalarSizeInBits()) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 85 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); |
| 86 | |
| 87 | return New; |
| 88 | } |
| 89 | |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 90 | // Or it is probably a scop-constant value defined as global, function |
| 91 | // parameter or an instruction not within the scop. |
| 92 | if (isa<GlobalValue>(Old) || isa<Argument>(Old)) |
| 93 | return const_cast<Value *>(Old); |
| 94 | |
| 95 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) |
| 96 | if (!Statement.getParent()->getRegion().contains(Inst->getParent())) |
| 97 | return const_cast<Value *>(Old); |
| 98 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 99 | if (Value *New = BBMap.lookup(Old)) |
| 100 | return New; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 101 | |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 102 | return nullptr; |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, |
| 106 | ValueMapT &GlobalMap, LoopToScevMapT <S, |
| 107 | Loop *L) { |
| 108 | if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap)) |
| 109 | return New; |
| 110 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 111 | if (SCEVCodegen && SE.isSCEVable(Old->getType())) |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 112 | if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 113 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 114 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 115 | ValueToValueMap VTV; |
| 116 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 117 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 118 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 119 | SCEVExpander Expander(SE, "polly"); |
| 120 | Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), |
| 121 | Builder.GetInsertPoint()); |
| 122 | |
| 123 | BBMap[Old] = Expanded; |
| 124 | return Expanded; |
| 125 | } |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 126 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 127 | |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 128 | // Now the scalar dependence is neither available nor SCEVCodegenable, this |
| 129 | // should never happen in the current code generator. |
| 130 | llvm_unreachable("Unexpected scalar dependence in region!"); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 131 | return nullptr; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 135 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 136 | // We do not generate debug intrinsics as we did not investigate how to |
| 137 | // copy them correctly. At the current state, they just crash the code |
| 138 | // generation as the meta-data operands are not correctly copied. |
| 139 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 140 | return; |
| 141 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 142 | Instruction *NewInst = Inst->clone(); |
| 143 | |
| 144 | // Replace old operands with the new ones. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 145 | for (Value *OldOperand : Inst->operands()) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 146 | Value *NewOperand = |
| 147 | getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 148 | |
| 149 | if (!NewOperand) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 150 | assert(!isa<StoreInst>(NewInst) && |
| 151 | "Store instructions are always needed!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 152 | delete NewInst; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 157 | } |
| 158 | |
| 159 | Builder.Insert(NewInst); |
| 160 | BBMap[Inst] = NewInst; |
| 161 | |
| 162 | if (!NewInst->getType()->isVoidTy()) |
| 163 | NewInst->setName("p_" + Inst->getName()); |
| 164 | } |
| 165 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 166 | Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) { |
| 167 | isl_pw_multi_aff *PWSchedule, *PWAccRel; |
| 168 | isl_union_map *ScheduleU; |
| 169 | isl_map *Schedule, *AccRel; |
| 170 | isl_ast_expr *Expr; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 171 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 172 | assert(ExprBuilder && Build && |
| 173 | "Cannot generate new value without IslExprBuilder!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 174 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 175 | AccRel = MA.getNewAccessRelation(); |
| 176 | assert(AccRel && "We generate new code only for new access relations!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 177 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 178 | ScheduleU = isl_ast_build_get_schedule(Build); |
| 179 | ScheduleU = isl_union_map_intersect_domain( |
| 180 | ScheduleU, isl_union_set_from_set(MA.getStatement()->getDomain())); |
| 181 | Schedule = isl_map_from_union_map(ScheduleU); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 182 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 183 | PWSchedule = isl_pw_multi_aff_from_map(isl_map_reverse(Schedule)); |
| 184 | PWAccRel = isl_pw_multi_aff_from_map(AccRel); |
| 185 | PWAccRel = isl_pw_multi_aff_pullback_pw_multi_aff(PWAccRel, PWSchedule); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 186 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 187 | Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); |
| 188 | |
| 189 | return ExprBuilder->create(Expr); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 192 | Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst, |
| 193 | const Value *Pointer, |
| 194 | ValueMapT &BBMap, |
| 195 | ValueMapT &GlobalMap, |
| 196 | LoopToScevMapT <S) { |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 197 | const MemoryAccess &MA = Statement.getAccessFor(Inst); |
| 198 | isl_map *NewAccRel = MA.getNewAccessRelation(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 199 | |
| 200 | Value *NewPointer; |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 201 | if (NewAccRel) |
| 202 | NewPointer = getNewAccessOperand(MA); |
| 203 | else |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 204 | NewPointer = |
| 205 | getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 206 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 207 | isl_map_free(NewAccRel); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 208 | return NewPointer; |
| 209 | } |
| 210 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 211 | Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 212 | return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent()); |
| 213 | } |
| 214 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 215 | Value *BlockGenerator::generateScalarLoad(const LoadInst *Load, |
| 216 | ValueMapT &BBMap, |
| 217 | ValueMapT &GlobalMap, |
| 218 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 219 | const Value *Pointer = Load->getPointerOperand(); |
| 220 | const Instruction *Inst = dyn_cast<Instruction>(Load); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 221 | Value *NewPointer = |
| 222 | generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 223 | Value *ScalarLoad = |
| 224 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 225 | return ScalarLoad; |
| 226 | } |
| 227 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 228 | Value *BlockGenerator::generateScalarStore(const StoreInst *Store, |
| 229 | ValueMapT &BBMap, |
| 230 | ValueMapT &GlobalMap, |
| 231 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 232 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 233 | Value *NewPointer = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 234 | generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 235 | Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, |
| 236 | LTS, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 237 | |
| 238 | return Builder.CreateStore(ValueOperand, NewPointer); |
| 239 | } |
| 240 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 241 | void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, |
| 242 | ValueMapT &GlobalMap, |
| 243 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 244 | // Terminator instructions control the control flow. They are explicitly |
| 245 | // expressed in the clast and do not need to be copied. |
| 246 | if (Inst->isTerminator()) |
| 247 | return; |
| 248 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 249 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 250 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 251 | return; |
| 252 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 253 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 254 | Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS); |
Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 255 | // Compute NewLoad before its insertion in BBMap to make the insertion |
| 256 | // deterministic. |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 257 | BBMap[Load] = NewLoad; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 262 | Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS); |
Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 263 | // Compute NewStore before its insertion in BBMap to make the insertion |
| 264 | // deterministic. |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 265 | BBMap[Store] = NewStore; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 269 | copyInstScalar(Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 272 | void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 273 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 274 | BasicBlock *CopyBB = |
| 275 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 276 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 277 | Builder.SetInsertPoint(CopyBB->begin()); |
| 278 | |
| 279 | ValueMapT BBMap; |
| 280 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 281 | for (Instruction &Inst : *BB) |
| 282 | copyInstruction(&Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 285 | VectorBlockGenerator::VectorBlockGenerator(PollyIRBuilder &B, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 286 | VectorValueMapT &GlobalMaps, |
| 287 | std::vector<LoopToScevMapT> &VLTS, |
| 288 | ScopStmt &Stmt, |
| 289 | __isl_keep isl_map *Schedule, |
| 290 | Pass *P) |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 291 | : BlockGenerator(B, Stmt, P, nullptr, nullptr), GlobalMaps(GlobalMaps), |
| 292 | VLTS(VLTS), Schedule(Schedule) { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 293 | assert(GlobalMaps.size() > 1 && "Only one vector lane found"); |
| 294 | assert(Schedule && "No statement domain provided"); |
| 295 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 296 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 297 | Value *VectorBlockGenerator::getVectorValue(const Value *Old, |
| 298 | ValueMapT &VectorMap, |
| 299 | VectorValueMapT &ScalarMaps, |
| 300 | Loop *L) { |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 301 | if (Value *NewValue = VectorMap.lookup(Old)) |
| 302 | return NewValue; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 303 | |
| 304 | int Width = getVectorWidth(); |
| 305 | |
| 306 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); |
| 307 | |
| 308 | for (int Lane = 0; Lane < Width; Lane++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 309 | Vector = Builder.CreateInsertElement( |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 310 | Vector, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 311 | getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L), |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 312 | Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 313 | |
| 314 | VectorMap[Old] = Vector; |
| 315 | |
| 316 | return Vector; |
| 317 | } |
| 318 | |
| 319 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 320 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 321 | assert(PointerTy && "PointerType expected"); |
| 322 | |
| 323 | Type *ScalarType = PointerTy->getElementType(); |
| 324 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 325 | |
| 326 | return PointerType::getUnqual(VectorType); |
| 327 | } |
| 328 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 329 | Value * |
| 330 | VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, |
| 331 | VectorValueMapT &ScalarMaps, |
| 332 | bool NegativeStride = false) { |
| 333 | unsigned VectorWidth = getVectorWidth(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 334 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 335 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
| 336 | unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; |
| 337 | |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 338 | Value *NewPointer = nullptr; |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 339 | NewPointer = getNewValue(Pointer, ScalarMaps[Offset], GlobalMaps[Offset], |
| 340 | VLTS[Offset], getLoopForInst(Load)); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 341 | Value *VectorPtr = |
| 342 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 343 | LoadInst *VecLoad = |
| 344 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 345 | if (!Aligned) |
| 346 | VecLoad->setAlignment(8); |
| 347 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 348 | if (NegativeStride) { |
| 349 | SmallVector<Constant *, 16> Indices; |
| 350 | for (int i = VectorWidth - 1; i >= 0; i--) |
| 351 | Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); |
| 352 | Constant *SV = llvm::ConstantVector::get(Indices); |
| 353 | Value *RevVecLoad = Builder.CreateShuffleVector( |
| 354 | VecLoad, VecLoad, SV, Load->getName() + "_reverse"); |
| 355 | return RevVecLoad; |
| 356 | } |
| 357 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 358 | return VecLoad; |
| 359 | } |
| 360 | |
| 361 | Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, |
| 362 | ValueMapT &BBMap) { |
| 363 | const Value *Pointer = Load->getPointerOperand(); |
| 364 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 365 | Value *NewPointer = |
| 366 | getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 367 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 368 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 369 | LoadInst *ScalarLoad = |
| 370 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 371 | |
| 372 | if (!Aligned) |
| 373 | ScalarLoad->setAlignment(8); |
| 374 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 375 | Constant *SplatVector = Constant::getNullValue( |
| 376 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 377 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 378 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 379 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 380 | return VectorLoad; |
| 381 | } |
| 382 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 383 | Value * |
| 384 | VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load, |
| 385 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 386 | int VectorWidth = getVectorWidth(); |
| 387 | const Value *Pointer = Load->getPointerOperand(); |
| 388 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 389 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 390 | |
| 391 | Value *Vector = UndefValue::get(VectorType); |
| 392 | |
| 393 | for (int i = 0; i < VectorWidth; i++) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 394 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 395 | VLTS[i], getLoopForInst(Load)); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 396 | Value *ScalarLoad = |
| 397 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 398 | Vector = Builder.CreateInsertElement( |
| 399 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return Vector; |
| 403 | } |
| 404 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 405 | void VectorBlockGenerator::generateLoad(const LoadInst *Load, |
| 406 | ValueMapT &VectorMap, |
| 407 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 408 | if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || |
| 409 | !VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 410 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 411 | ScalarMaps[i][Load] = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 412 | generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
Hongbin Zheng | b5f24a6 | 2013-06-29 06:31:39 +0000 | [diff] [blame] | 416 | const MemoryAccess &Access = Statement.getAccessFor(Load); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 417 | |
Tobias Grosser | 9549398 | 2014-04-18 09:46:35 +0000 | [diff] [blame] | 418 | // Make sure we have scalar values available to access the pointer to |
| 419 | // the data location. |
| 420 | extractScalarValues(Load, VectorMap, ScalarMaps); |
| 421 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 422 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 423 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 424 | NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 425 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 426 | NewLoad = generateStrideOneLoad(Load, ScalarMaps); |
| 427 | else if (Access.isStrideX(isl_map_copy(Schedule), -1)) |
| 428 | NewLoad = generateStrideOneLoad(Load, ScalarMaps, true); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 429 | else |
| 430 | NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); |
| 431 | |
| 432 | VectorMap[Load] = NewLoad; |
| 433 | } |
| 434 | |
| 435 | void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, |
| 436 | ValueMapT &VectorMap, |
| 437 | VectorValueMapT &ScalarMaps) { |
| 438 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 439 | Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps, |
| 440 | getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 441 | |
| 442 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 443 | |
| 444 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 445 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 446 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 447 | } |
| 448 | |
| 449 | void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, |
| 450 | ValueMapT &VectorMap, |
| 451 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 452 | Loop *L = getLoopForInst(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 453 | Value *OpZero = Inst->getOperand(0); |
| 454 | Value *OpOne = Inst->getOperand(1); |
| 455 | |
| 456 | Value *NewOpZero, *NewOpOne; |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 457 | NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L); |
| 458 | NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 459 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 460 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 461 | Inst->getName() + "p_vec"); |
| 462 | VectorMap[Inst] = NewInst; |
| 463 | } |
| 464 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 465 | void VectorBlockGenerator::copyStore(const StoreInst *Store, |
| 466 | ValueMapT &VectorMap, |
| 467 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 468 | int VectorWidth = getVectorWidth(); |
| 469 | |
Hongbin Zheng | b5f24a6 | 2013-06-29 06:31:39 +0000 | [diff] [blame] | 470 | const MemoryAccess &Access = Statement.getAccessFor(Store); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 471 | |
| 472 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 473 | Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap, |
| 474 | ScalarMaps, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 475 | |
Tobias Grosser | 50fd701 | 2014-04-17 23:13:49 +0000 | [diff] [blame] | 476 | // Make sure we have scalar values available to access the pointer to |
| 477 | // the data location. |
| 478 | extractScalarValues(Store, VectorMap, ScalarMaps); |
| 479 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 480 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 481 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 482 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], |
| 483 | VLTS[0], getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 484 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 485 | Value *VectorPtr = |
| 486 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 487 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 488 | |
| 489 | if (!Aligned) |
| 490 | Store->setAlignment(8); |
| 491 | } else { |
| 492 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 493 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 494 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 495 | VLTS[i], getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 496 | Builder.CreateStore(Scalar, NewPointer); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 502 | ValueMapT &VectorMap) { |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 503 | for (Value *Operand : Inst->operands()) |
| 504 | if (VectorMap.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 505 | return true; |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 510 | ValueMapT &VectorMap, |
| 511 | VectorValueMapT &ScalarMaps) { |
| 512 | bool HasVectorOperand = false; |
| 513 | int VectorWidth = getVectorWidth(); |
| 514 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 515 | for (Value *Operand : Inst->operands()) { |
| 516 | ValueMapT::iterator VecOp = VectorMap.find(Operand); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 517 | |
| 518 | if (VecOp == VectorMap.end()) |
| 519 | continue; |
| 520 | |
| 521 | HasVectorOperand = true; |
| 522 | Value *NewVector = VecOp->second; |
| 523 | |
| 524 | for (int i = 0; i < VectorWidth; ++i) { |
| 525 | ValueMapT &SM = ScalarMaps[i]; |
| 526 | |
| 527 | // If there is one scalar extracted, all scalar elements should have |
| 528 | // already been extracted by the code here. So no need to check for the |
| 529 | // existance of all of them. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 530 | if (SM.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 531 | break; |
| 532 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 533 | SM[Operand] = |
| 534 | Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | return HasVectorOperand; |
| 539 | } |
| 540 | |
| 541 | void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, |
| 542 | ValueMapT &VectorMap, |
| 543 | VectorValueMapT &ScalarMaps) { |
| 544 | bool HasVectorOperand; |
| 545 | int VectorWidth = getVectorWidth(); |
| 546 | |
| 547 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 548 | |
| 549 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 550 | copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane], |
| 551 | VLTS[VectorLane]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 552 | |
| 553 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 554 | return; |
| 555 | |
| 556 | // Make the result available as vector value. |
| 557 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 558 | Value *Vector = UndefValue::get(VectorType); |
| 559 | |
| 560 | for (int i = 0; i < VectorWidth; i++) |
| 561 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 562 | Builder.getInt32(i)); |
| 563 | |
| 564 | VectorMap[Inst] = Vector; |
| 565 | } |
| 566 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 567 | int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 568 | |
| 569 | void VectorBlockGenerator::copyInstruction(const Instruction *Inst, |
| 570 | ValueMapT &VectorMap, |
| 571 | VectorValueMapT &ScalarMaps) { |
| 572 | // Terminator instructions control the control flow. They are explicitly |
| 573 | // expressed in the clast and do not need to be copied. |
| 574 | if (Inst->isTerminator()) |
| 575 | return; |
| 576 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 577 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 578 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 579 | return; |
| 580 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 581 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 582 | generateLoad(Load, VectorMap, ScalarMaps); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (hasVectorOperands(Inst, VectorMap)) { |
| 587 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 588 | copyStore(Store, VectorMap, ScalarMaps); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
| 593 | copyUnaryInst(Unary, VectorMap, ScalarMaps); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
| 598 | copyBinaryInst(Binary, VectorMap, ScalarMaps); |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 603 | // generate vector code. |
| 604 | } |
| 605 | |
| 606 | copyInstScalarized(Inst, VectorMap, ScalarMaps); |
| 607 | } |
| 608 | |
| 609 | void VectorBlockGenerator::copyBB() { |
| 610 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 611 | BasicBlock *CopyBB = |
| 612 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 613 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 614 | Builder.SetInsertPoint(CopyBB->begin()); |
| 615 | |
| 616 | // Create two maps that store the mapping from the original instructions of |
| 617 | // the old basic block to their copies in the new basic block. Those maps |
| 618 | // are basic block local. |
| 619 | // |
| 620 | // As vector code generation is supported there is one map for scalar values |
| 621 | // and one for vector values. |
| 622 | // |
| 623 | // In case we just do scalar code generation, the vectorMap is not used and |
| 624 | // the scalarMap has just one dimension, which contains the mapping. |
| 625 | // |
| 626 | // In case vector code generation is done, an instruction may either appear |
| 627 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 628 | // time. Or (if the values are calculated using scalar operations), it |
| 629 | // appears once in every dimension of the scalarMap. |
| 630 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 631 | ValueMapT VectorBlockMap; |
| 632 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 633 | for (Instruction &Inst : *BB) |
| 634 | copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 635 | } |