add a new SimplifyInstruction API, which is like ConstantFoldInstruction,
authorChris Lattner <sabre@nondot.org>
Tue, 10 Nov 2009 01:08:51 +0000 (01:08 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 10 Nov 2009 01:08:51 +0000 (01:08 +0000)
except that the result may not be a constant.  Switch jump threading to
use it so that it gets things like (X & 0) -> 0, which occur when phi preds
are deleted and the remaining phi pred was a zero.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86637 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/InstructionSimplify.h
lib/Analysis/InstructionSimplify.cpp
lib/Transforms/Scalar/JumpThreading.cpp

index 2398cf694fa831ef24f39f275686415b07fff7d4..bb5c811fe2e20a3209c91dcd1f186c260569a6b5 100644 (file)
 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
 
 namespace llvm {
+  class Instruction;
   class Value;
   class TargetData;
   
-  
   /// SimplifyAndInst - Given operands for an And, see if we can
   /// fold the result.  If not, this returns null.
   Value *SimplifyAndInst(Value *LHS, Value *RHS,
@@ -55,6 +55,10 @@ namespace llvm {
   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, 
                        const TargetData *TD = 0);
   
+  /// SimplifyInstruction - See if we can compute a simplified version of this
+  /// instruction.  If not, this returns null.
+  Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0);
+  
 } // end namespace llvm
 
 #endif
index 3c1529ce6df6174b67ca51c741d88562b30a63da..6953f16dc929a1b470a6da5d40d18268332d5878 100644 (file)
@@ -291,3 +291,23 @@ Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
 }
 
+
+/// SimplifyInstruction - See if we can compute a simplified version of this
+/// instruction.  If not, this returns null.
+Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
+  switch (I->getOpcode()) {
+  default:
+    return ConstantFoldInstruction(I, TD);
+  case Instruction::And:
+    return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
+  case Instruction::Or:
+    return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
+  case Instruction::ICmp:
+    return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
+                            I->getOperand(0), I->getOperand(1), TD);
+  case Instruction::FCmp:
+    return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
+                            I->getOperand(0), I->getOperand(1), TD);
+  }
+}
+
index 7eaae9b36f1d991ab27e9393a3d62f4838454c3f..46d40ef5d1564ac6deb2dca605c79f898be2ca76 100644 (file)
@@ -16,7 +16,6 @@
 #include "llvm/IntrinsicInst.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Pass.h"
-#include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
@@ -223,9 +222,9 @@ static void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
       Instruction *User = cast<Instruction>(U.getUser());
       U = PNV;
       
-      // See if we can simplify it (constant folding).
-      if (Constant *C = ConstantFoldInstruction(User, TD)) {
-        User->replaceAllUsesWith(C);
+      // See if we can simplify it.
+      if (Value *V = SimplifyInstruction(User, TD)) {
+        User->replaceAllUsesWith(V);
         User->eraseFromParent();
       }
     }
@@ -1203,8 +1202,8 @@ bool JumpThreading::ThreadEdge(BasicBlock *BB,
   BI = NewBB->begin();
   for (BasicBlock::iterator E = NewBB->end(); BI != E; ) {
     Instruction *Inst = BI++;
-    if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
-      Inst->replaceAllUsesWith(C);
+    if (Value *V = SimplifyInstruction(Inst, TD)) {
+      Inst->replaceAllUsesWith(V);
       Inst->eraseFromParent();
       continue;
     }