Index: include/polly/CodeGen/BlockGenerators.h =================================================================== --- include/polly/CodeGen/BlockGenerators.h +++ include/polly/CodeGen/BlockGenerators.h @@ -535,22 +535,6 @@ void copyInstruction(ScopStmt &Stmt, Instruction *Inst, ValueMapT &BBMap, LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses); - /// @brief Helper to get the newest version of @p ScalarValue. - /// - /// @param ScalarValue The original value needed. - /// @param R The current SCoP region. - /// @param Stmt The ScopStmt in which we look up this value. - /// @param LTS A mapping from loops virtual canonical induction - /// variable to their new values - /// (for values recalculated in the new ScoP, but not - /// within this basic block) - /// @param BBMap A mapping from old values to their new values - /// (for values recalculated within this basic block). - /// - /// @returns The newest version (e.g., reloaded) of the scalar value. - Value *getNewScalarValue(Value *ScalarValue, const Region &R, ScopStmt &, - LoopToScevMapT <S, ValueMapT &BBMap); - /// @brief Helper to determine if @p Inst can be synthezised in @p Stmt. /// /// @returns false, iff @p Inst can be synthesized in @p Stmt. Index: lib/CodeGen/BlockGenerators.cpp =================================================================== --- lib/CodeGen/BlockGenerators.cpp +++ lib/CodeGen/BlockGenerators.cpp @@ -404,62 +404,22 @@ if (MA->isArrayKind() || MA->isWrite()) continue; + // Scalars that are already available in GlobalMap do not need to be loaded + // as they values available in GlobalMap are always considered before + // looking up values in BBMap. This is e.g. used to make scop invariant + // values available. + if (MA->isScalarKind() && GlobalMap.count(MA->getBaseAddr())) + continue; + auto *Address = getOrCreateAlloca(*MA); BBMap[MA->getBaseAddr()] = Builder.CreateLoad(Address, Address->getName() + ".reload"); } } -Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R, - ScopStmt &Stmt, LoopToScevMapT <S, - ValueMapT &BBMap) { - // If the value we want to store is an instruction we might have demoted it - // in order to make it accessible here. In such a case a reload is - // necessary. If it is no instruction it will always be a value that - // dominates the current point and we can just use it. In total there are 4 - // options: - // (1) The value is no instruction ==> use the value. - // (2) The value is an instruction that was split out of the region prior to - // code generation ==> use the instruction as it dominates the region. - // (3) The value is an instruction: - // (a) The value was defined in the current block, thus a copy is in - // the BBMap ==> use the mapped value. - // (b) The value was defined in a previous block, thus we demoted it - // earlier ==> use the reloaded value. - Instruction *ScalarValueInst = dyn_cast(ScalarValue); - if (!ScalarValueInst) - return ScalarValue; - - if (!R.contains(ScalarValueInst)) { - if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst)) - return /* Case (3a) */ ScalarValueCopy; - else - return /* Case 2 */ ScalarValue; - } - - if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst)) - return /* Case (3a) */ ScalarValueCopy; - - if ((Stmt.isBlockStmt() && - Stmt.getBasicBlock() == ScalarValueInst->getParent()) || - (Stmt.isRegionStmt() && Stmt.getRegion()->contains(ScalarValueInst))) { - auto SynthesizedValue = trySynthesizeNewValue( - Stmt, ScalarValueInst, BBMap, LTS, getLoopForInst(ScalarValueInst)); - - if (SynthesizedValue) - return SynthesizedValue; - } - - // Case (3b) - Value *Address = getOrCreateScalarAlloca(ScalarValueInst); - ScalarValue = Builder.CreateLoad(Address, Address->getName() + ".reload"); - - return ScalarValue; -} - void BlockGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap) { - const Region &R = Stmt.getParent()->getRegion(); + Loop *L = LI.getLoopFor(Stmt.getBasicBlock()); assert(Stmt.isBlockStmt() && "Region statements need to use the " "generateScalarStores() function in the " @@ -472,7 +432,7 @@ Value *Val = MA->getAccessValue(); auto *Address = getOrCreateAlloca(*MA); - Val = getNewScalarValue(Val, R, Stmt, LTS, BBMap); + Val = getNewValue(Stmt, Val, BBMap, LTS, L); Builder.CreateStore(Val, Address); } } @@ -1228,7 +1188,6 @@ void RegionGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap) { - const Region &R = Stmt.getParent()->getRegion(); assert(Stmt.getRegion() && "Block statements need to use the generateScalarStores() " @@ -1260,7 +1219,8 @@ auto Address = getOrCreateAlloca(*MA); - Val = getNewScalarValue(Val, R, Stmt, LTS, *LocalBBMap); + Loop *L = getLoopForInst(ScalarInst); + Val = getNewValue(Stmt, Val, *LocalBBMap, LTS, L); Builder.CreateStore(Val, Address); // Restore the insertion point if necessary. Index: test/Isl/CodeGen/invariant_load_scalar_escape_alloca_sharing.ll =================================================================== --- test/Isl/CodeGen/invariant_load_scalar_escape_alloca_sharing.ll +++ test/Isl/CodeGen/invariant_load_scalar_escape_alloca_sharing.ll @@ -14,8 +14,7 @@ ; CHECK-NEXT: store i32 %ncol.load, i32* %tmp0.preload.s2a ; ; CHECK: polly.stmt.while.body.lr.ph: -; CHECK-NEXT: %tmp0.preload.s2a.reload = load i32, i32* %tmp0.preload.s2a -; CHECK-NEXT: store i32 %tmp0.preload.s2a.reload, i32* %dec3.in.phiops +; CHECK-NEXT: store i32 %ncol.load, i32* %dec3.in.phiops ; target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" Index: test/Isl/CodeGen/phi-defined-before-scop.ll =================================================================== --- test/Isl/CodeGen/phi-defined-before-scop.ll +++ test/Isl/CodeGen/phi-defined-before-scop.ll @@ -5,7 +5,7 @@ ; CHECK-LABEL: polly.stmt.bb3: ; CHECK-NEXT: %tmp2.s2a.reload = load %struct.wibble*, %struct.wibble** %tmp2.s2a -; CHECK-NEXT: store %struct.wibble* %tmp2, %struct.wibble** %tmp7.s2a +; CHECK-NEXT: store %struct.wibble* %tmp2.s2a.reload, %struct.wibble** %tmp7.s2a target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"