This ensures that no trivially dead code is generated. This is not only cleaner,
but also avoids troubles in case code is generated in a separate function and
some of this dead code contains references to values that are not available.
This issue may happen, in case the memory access functions have been updated
and old getelementptr instructions remain in the code. With normal Polly,
a test case is difficult to draft, but the upcoming GPU code generation can
possibly trigger such problems. We will later extend this dead-code elimination
to region and vector statements.
llvm-svn: 276263
///
/// @returns false, iff @p Inst can be synthesized in @p Stmt.
bool canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst);
+
+ /// @brief Remove dead instructions generated for BB
+ ///
+ /// @param BB The basic block code for which code has been generated.
+ /// @param BBMap A local map from old to new instructions.
+ void removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap);
};
/// @brief Generate a new vector basic block for a polyhedral statement.
copyInstScalar(Stmt, Inst, BBMap, LTS);
}
+void BlockGenerator::removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap) {
+ for (auto I = BB->rbegin(), E = BB->rend(); I != E; I++) {
+ Instruction *Inst = &*I;
+ Value *NewVal = BBMap[Inst];
+
+ if (!NewVal)
+ continue;
+
+ Instruction *NewInst = dyn_cast<Instruction>(NewVal);
+
+ if (!NewInst)
+ continue;
+
+ if (!isInstructionTriviallyDead(NewInst))
+ continue;
+
+ BBMap.erase(Inst);
+ NewInst->eraseFromParent();
+ }
+}
+
void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S,
isl_id_to_ast_expr *NewAccesses) {
assert(Stmt.isBlockStmt() &&
BasicBlock *BB = Stmt.getBasicBlock();
copyBB(Stmt, BB, BBMap, LTS, NewAccesses);
+ removeDeadInstructions(BB, BBMap);
}
BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
; IR: polly.stmt.for.body:
; IR-NEXT: %p_tmp = ptrtoint i64* %scevgep to i64
; IR-NEXT: %p_add = add nsw i64 %p_tmp, 1
-; IR-NEXT: %p_tmp1 = inttoptr i64 %26 to i64*
-; IR-NEXT: %p_add.ptr2 = getelementptr inbounds i64, i64* %p_tmp1, i64 1
-; IR-NEXT: %p_tmp2 = ptrtoint i64* %p_add.ptr2 to i64
-; IR-NEXT: %p_arrayidx = getelementptr inbounds i64, i64* %B, i64 %p_tmp2
; IR-NEXT: %p_arrayidx3 = getelementptr inbounds i64, i64* %A, i64 %p_add
; IR-NEXT: %tmp4_p_scalar_ = load i64, i64* %p_arrayidx3
; IR-NEXT: %p_add4 = add nsw i64 %tmp4_p_scalar_, %polly.preload.tmp3.merge
;
; IR: polly.loop_preheader:
; IR-NEXT: %scevgep = getelementptr i64, i64* %ptr, i64 1
-; IR-NEXT: %26 = add i64 %val, 1
; IR-NEXT: br label %polly.loop_header
;
;