From 9d12d8ade38c1975b68c450208b734f66637c068 Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Thu, 21 Jul 2016 11:48:36 +0000 Subject: [PATCH] BlockGenerator: remove dead instructions in normal statements 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 --- polly/include/polly/CodeGen/BlockGenerators.h | 6 ++++++ polly/lib/CodeGen/BlockGenerators.cpp | 22 ++++++++++++++++++++++ polly/test/ScopInfo/int2ptr_ptr2int_2.ll | 5 ----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/polly/include/polly/CodeGen/BlockGenerators.h b/polly/include/polly/CodeGen/BlockGenerators.h index 5efb596..eade339 100644 --- a/polly/include/polly/CodeGen/BlockGenerators.h +++ b/polly/include/polly/CodeGen/BlockGenerators.h @@ -540,6 +540,12 @@ protected: /// /// @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. diff --git a/polly/lib/CodeGen/BlockGenerators.cpp b/polly/lib/CodeGen/BlockGenerators.cpp index 3904e85..9d0517d5f 100644 --- a/polly/lib/CodeGen/BlockGenerators.cpp +++ b/polly/lib/CodeGen/BlockGenerators.cpp @@ -278,6 +278,27 @@ void BlockGenerator::copyInstruction(ScopStmt &Stmt, Instruction *Inst, 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(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() && @@ -287,6 +308,7 @@ void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, BasicBlock *BB = Stmt.getBasicBlock(); copyBB(Stmt, BB, BBMap, LTS, NewAccesses); + removeDeadInstructions(BB, BBMap); } BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { diff --git a/polly/test/ScopInfo/int2ptr_ptr2int_2.ll b/polly/test/ScopInfo/int2ptr_ptr2int_2.ll index e47e181..16cd32f 100644 --- a/polly/test/ScopInfo/int2ptr_ptr2int_2.ll +++ b/polly/test/ScopInfo/int2ptr_ptr2int_2.ll @@ -21,10 +21,6 @@ ; 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 @@ -32,7 +28,6 @@ ; ; 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 ; ; -- 2.7.4