Implement combination of boolean not with branch
authorChris Lattner <sabre@nondot.org>
Wed, 4 Jun 2003 04:46:00 +0000 (04:46 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 4 Jun 2003 04:46:00 +0000 (04:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6599 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index 21c58b696e0c42a3abef377d37f4a5c41bd8504f..2a5856ae37cbc90c29712fae6f0359b8ce218859 100644 (file)
@@ -76,6 +76,7 @@ namespace {
     Instruction *visitPHINode(PHINode &PN);
     Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
     Instruction *visitAllocationInst(AllocationInst &AI);
+    Instruction *visitBranchInst(BranchInst &BI);
 
     // visitInstruction - Specify what to return for unhandled instructions...
     Instruction *visitInstruction(Instruction &I) { return 0; }
@@ -1061,6 +1062,19 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
   return 0;
 }
 
+Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
+  // Change br (not X), label True, label False to: br X, label False, True
+  if (BI.isConditional() && BinaryOperator::isNot(BI.getCondition())) {
+    BasicBlock *TrueDest = BI.getSuccessor(0);
+    BasicBlock *FalseDest = BI.getSuccessor(1);
+    // Swap Destinations and condition...
+    BI.setCondition(BinaryOperator::getNotArgument(cast<BinaryOperator>(BI.getCondition())));
+    BI.setSuccessor(0, FalseDest);
+    BI.setSuccessor(1, TrueDest);
+    return &BI;
+  }
+  return 0;
+}
 
 
 void InstCombiner::removeFromWorkList(Instruction *I) {