BlockGenerator: remove dead instructions in normal statements
authorTobias Grosser <tobias@grosser.es>
Thu, 21 Jul 2016 11:48:36 +0000 (11:48 +0000)
committerTobias Grosser <tobias@grosser.es>
Thu, 21 Jul 2016 11:48:36 +0000 (11:48 +0000)
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
polly/lib/CodeGen/BlockGenerators.cpp
polly/test/ScopInfo/int2ptr_ptr2int_2.ll

index 5efb596..eade339 100644 (file)
@@ -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.
index 3904e85..9d0517d 100644 (file)
@@ -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<Instruction>(NewVal);
+
+    if (!NewInst)
+      continue;
+
+    if (!isInstructionTriviallyDead(NewInst))
+      continue;
+
+    BBMap.erase(Inst);
+    NewInst->eraseFromParent();
+  }
+}
+
 void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
                               isl_id_to_ast_expr *NewAccesses) {
   assert(Stmt.isBlockStmt() &&
@@ -287,6 +308,7 @@ void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
 
   BasicBlock *BB = Stmt.getBasicBlock();
   copyBB(Stmt, BB, BBMap, LTS, NewAccesses);
+  removeDeadInstructions(BB, BBMap);
 }
 
 BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
index e47e181..16cd32f 100644 (file)
 ; 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
 ;
 ;