From d76956d4441e790828571778281671c677078576 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 10 Apr 2004 22:21:27 +0000 Subject: [PATCH] Implement InstCombine/select.ll:test13* git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12821 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 23d0f5ffcc7..56c52b3fb81 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2209,6 +2209,28 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { return new CastInst(NotCond, SI.getType()); } } + + // See if we are selecting two values based on a comparison of the two values. + if (SetCondInst *SCI = dyn_cast(CondVal)) { + if (SCI->getOperand(0) == TrueVal && SCI->getOperand(1) == FalseVal) { + // Transform (X == Y) ? X : Y -> Y + if (SCI->getOpcode() == Instruction::SetEQ) + return ReplaceInstUsesWith(SI, FalseVal); + // Transform (X != Y) ? X : Y -> X + if (SCI->getOpcode() == Instruction::SetNE) + return ReplaceInstUsesWith(SI, TrueVal); + // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. + + } else if (SCI->getOperand(0) == FalseVal && SCI->getOperand(1) == TrueVal){ + // Transform (X == Y) ? Y : X -> X + if (SCI->getOpcode() == Instruction::SetEQ) + return ReplaceInstUsesWith(SI, TrueVal); + // Transform (X != Y) ? Y : X -> Y + if (SCI->getOpcode() == Instruction::SetNE) + return ReplaceInstUsesWith(SI, FalseVal); + // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. + } + } // See if we can fold the select into one of our operands. if (SI.getType()->isInteger()) {