diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-11-01 17:46:08 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-11-01 17:46:08 +0000 |
commit | 7ce658388b259863d172a3d6821f7b5cb47494f1 (patch) | |
tree | 9d0df83b7f319501e79cda0c17f88d70c2472e3e /llvm/lib | |
parent | 61e900b4057dff9c9a5d7e8b0a2cffd2e861414b (diff) | |
download | bcm5719-llvm-7ce658388b259863d172a3d6821f7b5cb47494f1.tar.gz bcm5719-llvm-7ce658388b259863d172a3d6821f7b5cb47494f1.zip |
[InstCombine] add helper function for adjustMinMax(); NFCI
This is just a cut and paste; clean-up and enhancements to follow.
llvm-svn: 285715
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index af6b013aa34..27b2894de4f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -413,9 +413,10 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal, return nullptr; } -/// Visit a SelectInst that has an ICmpInst as its first operand. -Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, - ICmpInst *ICI) { +/// Return true if we find and adjust an icmp+select pattern where the compare +/// is with a constant that can be incremented or decremented to match the +/// minimum or maximum idiom. +static bool adjustMinMax(SelectInst &SI, ICmpInst *ICI) { bool Changed = false; ICmpInst::Predicate Pred = ICI->getPredicate(); Value *CmpLHS = ICI->getOperand(0); @@ -423,9 +424,7 @@ Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, Value *TrueVal = SI.getTrueValue(); Value *FalseVal = SI.getFalseValue(); - // Check cases where the comparison is with a constant that - // can be adjusted to fit the min/max idiom. We may move or edit ICI - // here, so make sure the select is the only user. + // We may move or edit ICI here, so make sure the select is the only user. if (ICI->hasOneUse()) if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) { switch (Pred) { @@ -511,6 +510,20 @@ Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, } } + return Changed; +} + +/// Visit a SelectInst that has an ICmpInst as its first operand. +Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, + ICmpInst *ICI) { + bool Changed = adjustMinMax(SI, ICI); + + ICmpInst::Predicate Pred = ICI->getPredicate(); + Value *CmpLHS = ICI->getOperand(0); + Value *CmpRHS = ICI->getOperand(1); + Value *TrueVal = SI.getTrueValue(); + Value *FalseVal = SI.getFalseValue(); + // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1 // and (X <s 0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1 // FIXME: Type and constness constraints could be lifted, but we have to |