[llvm-commits] [llvm] r139113 - in /llvm/trunk: include/llvm/Analysis/ConstantFolding.h include/llvm/Analysis/InstructionSimplify.h lib/Analysis/InstructionSimplify.cpp test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll

Duncan Sands baldrick at free.fr
Sun Sep 4 23:52:49 PDT 2011


Author: baldrick
Date: Mon Sep  5 01:52:48 2011
New Revision: 139113

URL: http://llvm.org/viewvc/llvm-project?rev=139113&view=rev
Log:
Add some simple insertvalue simplifications, for the purpose of cleaning
up do-nothing exception handling code produced by dragonegg.

Added:
    llvm/trunk/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll
Modified:
    llvm/trunk/include/llvm/Analysis/ConstantFolding.h
    llvm/trunk/include/llvm/Analysis/InstructionSimplify.h
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp

Modified: llvm/trunk/include/llvm/Analysis/ConstantFolding.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ConstantFolding.h?rev=139113&r1=139112&r2=139113&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/ConstantFolding.h (original)
+++ llvm/trunk/include/llvm/Analysis/ConstantFolding.h Mon Sep  5 01:52:48 2011
@@ -61,6 +61,12 @@
                                           Constant *LHS, Constant *RHS,
                                           const TargetData *TD = 0);
 
+/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
+/// instruction with the specified operands and indices.  The constant result is
+/// returned if successful; if not, null is returned.
+Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
+                                             ArrayRef<unsigned> Idxs);
+
 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
 /// produce if it is constant and determinable.  If this is not determinable,
 /// return null.

Modified: llvm/trunk/include/llvm/Analysis/InstructionSimplify.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/InstructionSimplify.h?rev=139113&r1=139112&r2=139113&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/InstructionSimplify.h (original)
+++ llvm/trunk/include/llvm/Analysis/InstructionSimplify.h Mon Sep  5 01:52:48 2011
@@ -126,6 +126,13 @@
   Value *SimplifyGEPInst(ArrayRef<Value *> Ops,
                          const TargetData *TD = 0, const DominatorTree *DT = 0);
 
+  /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
+  /// can fold the result.  If not, this returns null.
+  Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
+                                 ArrayRef<unsigned> Idxs,
+                                 const TargetData *TD = 0,
+                                 const DominatorTree *DT = 0);
+
   //=== Helper functions for higher up the class hierarchy.
 
 

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=139113&r1=139112&r2=139113&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon Sep  5 01:52:48 2011
@@ -2270,6 +2270,35 @@
   return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1));
 }
 
+/// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
+/// can fold the result.  If not, this returns null.
+Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
+                                     ArrayRef<unsigned> Idxs,
+                                     const TargetData *,
+                                     const DominatorTree *) {
+  if (Constant *CAgg = dyn_cast<Constant>(Agg))
+    if (Constant *CVal = dyn_cast<Constant>(Val))
+      return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
+
+  // insertvalue x, undef, n -> x
+  if (match(Val, m_Undef()))
+    return Agg;
+
+  // insertvalue x, (extractvalue y, n), n
+  if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
+    if (EV->getIndices() == Idxs) {
+      // insertvalue undef, (extractvalue y, n), n -> y
+      if (match(Agg, m_Undef()))
+        return EV->getAggregateOperand();
+
+      // insertvalue y, (extractvalue y, n), n -> y
+      if (Agg == EV->getAggregateOperand())
+        return Agg;
+    }
+
+  return 0;
+}
+
 /// SimplifyPHINode - See if we can fold the given phi.  If not, returns null.
 static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) {
   // If all of the PHI's incoming values are the same then replace the PHI node
@@ -2471,6 +2500,13 @@
     Result = SimplifyGEPInst(Ops, TD, DT);
     break;
   }
+  case Instruction::InsertValue: {
+    InsertValueInst *IV = cast<InsertValueInst>(I);
+    Result = SimplifyInsertValueInst(IV->getAggregateOperand(),
+                                     IV->getInsertedValueOperand(),
+                                     IV->getIndices(), TD, DT);
+    break;
+  }
   case Instruction::PHI:
     Result = SimplifyPHINode(cast<PHINode>(I), DT);
     break;

Added: llvm/trunk/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll?rev=139113&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/2011-09-05-InsertExtractValue.ll Mon Sep  5 01:52:48 2011
@@ -0,0 +1,22 @@
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+; CHECK-NOT: extractvalue
+; CHECK-NOT: insertvalue
+
+declare void @bar()
+
+define void @foo() {
+entry:
+  invoke void @bar() to label %cont unwind label %lpad
+cont:
+  ret void
+lpad:
+  %ex = landingpad { i8*, i32 } personality i32 (i32, i64, i8*, i8*)* @__gxx_personality_v0 cleanup
+  %exc_ptr = extractvalue { i8*, i32 } %ex, 0
+  %filter = extractvalue { i8*, i32 } %ex, 1
+  %exc_ptr2 = insertvalue { i8*, i32 } undef, i8* %exc_ptr, 0
+  %filter2 = insertvalue { i8*, i32 } %exc_ptr2, i32 %filter, 1
+  resume { i8*, i32 } %filter2
+}
+
+declare i32 @__gxx_personality_v0(i32, i64, i8*, i8*)





More information about the llvm-commits mailing list