Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1 | //===- InstructionSimplify.cpp - Fold instruction operands ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements routines for folding instructions into simpler forms |
Duncan Sands | 4cd2ad1 | 2010-11-23 10:50:08 +0000 | [diff] [blame] | 11 | // that do not require creating new instructions. This does constant folding |
| 12 | // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either |
| 13 | // returning a constant ("and i32 %x, 0" -> "0") or an already existing value |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 14 | // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been |
| 15 | // simplified: This is usually true and assuming it simplifies the logic (if |
| 16 | // they have not been simplified then results are correct but maybe suboptimal). |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "instsimplify" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/InstructionSimplify.h" |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 29 | #include "llvm/Support/PatternMatch.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ValueHandle.h" |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 33 | using namespace llvm::PatternMatch; |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 35 | enum { RecursionLimit = 3 }; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 36 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 37 | STATISTIC(NumExpand, "Number of expansions"); |
| 38 | STATISTIC(NumFactor , "Number of factorizations"); |
| 39 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 40 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 41 | static Value *SimplifyAndInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 42 | const TargetLibraryInfo *, const DominatorTree *, |
| 43 | unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 44 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 45 | const TargetLibraryInfo *, const DominatorTree *, |
| 46 | unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 47 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 48 | const TargetLibraryInfo *, const DominatorTree *, |
| 49 | unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 50 | static Value *SimplifyOrInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 51 | const TargetLibraryInfo *, const DominatorTree *, |
| 52 | unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 53 | static Value *SimplifyXorInst(Value *, Value *, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 54 | const TargetLibraryInfo *, const DominatorTree *, |
| 55 | unsigned); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 56 | |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 57 | /// getFalse - For a boolean type, or a vector of boolean type, return false, or |
| 58 | /// a vector with every element false, as appropriate for the type. |
| 59 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 60 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 61 | "Expected i1 type or a vector of i1!"); |
| 62 | return Constant::getNullValue(Ty); |
| 63 | } |
| 64 | |
| 65 | /// getTrue - For a boolean type, or a vector of boolean type, return true, or |
| 66 | /// a vector with every element true, as appropriate for the type. |
| 67 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 68 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 69 | "Expected i1 type or a vector of i1!"); |
| 70 | return Constant::getAllOnesValue(Ty); |
| 71 | } |
| 72 | |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 73 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 74 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 75 | Value *RHS) { |
| 76 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 77 | if (!Cmp) |
| 78 | return false; |
| 79 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 80 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 81 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 82 | return true; |
| 83 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 84 | CRHS == LHS; |
| 85 | } |
| 86 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 87 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 88 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 89 | Instruction *I = dyn_cast<Instruction>(V); |
| 90 | if (!I) |
| 91 | // Arguments and constants dominate all instructions. |
| 92 | return true; |
| 93 | |
| 94 | // If we have a DominatorTree then do a precise test. |
| 95 | if (DT) |
Rafael Espindola | 8c727f9 | 2012-02-26 01:50:14 +0000 | [diff] [blame] | 96 | return !DT->isReachableFromEntry(P->getParent()) || |
| 97 | !DT->isReachableFromEntry(I->getParent()) || DT->dominates(I, P); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 98 | |
| 99 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 100 | // then it obviously dominates all phi nodes. |
| 101 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 102 | !isa<InvokeInst>(I)) |
| 103 | return true; |
| 104 | |
| 105 | return false; |
| 106 | } |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 107 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 108 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 109 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 110 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 111 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 112 | /// Returns the simplified value, or null if no simplification was performed. |
| 113 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 114 | unsigned OpcToExpand, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 115 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
| 116 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 117 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 118 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 119 | if (!MaxRecurse--) |
| 120 | return 0; |
| 121 | |
| 122 | // Check whether the expression has the form "(A op' B) op C". |
| 123 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 124 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 125 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 126 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 127 | // Do "A op C" and "B op C" both simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 128 | if (Value *L = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse)) |
| 129 | if (Value *R = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 130 | // They do! Return "L op' R" if it simplifies or is already available. |
| 131 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 132 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 133 | && L == B && R == A)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 134 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 135 | return LHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 136 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 137 | // Otherwise return "L op' R" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 138 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT, |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 139 | MaxRecurse)) { |
| 140 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 141 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 142 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Check whether the expression has the form "A op (B op' C)". |
| 147 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 148 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 149 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 150 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 151 | // Do "A op B" and "A op C" both simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 152 | if (Value *L = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse)) |
| 153 | if (Value *R = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 154 | // They do! Return "L op' R" if it simplifies or is already available. |
| 155 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 156 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 157 | && L == C && R == B)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 158 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 159 | return RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 160 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 161 | // Otherwise return "L op' R" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 162 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT, |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 163 | MaxRecurse)) { |
| 164 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 165 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 166 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term |
| 174 | /// using the operation OpCodeToExtract. For example, when Opcode is Add and |
| 175 | /// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)". |
| 176 | /// Returns the simplified value, or null if no simplification was performed. |
| 177 | static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 178 | unsigned OpcToExtract, const TargetData *TD, |
| 179 | const TargetLibraryInfo *TLI, |
| 180 | const DominatorTree *DT, |
| 181 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 182 | Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 183 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 184 | if (!MaxRecurse--) |
| 185 | return 0; |
| 186 | |
| 187 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 188 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 189 | |
| 190 | if (!Op0 || Op0->getOpcode() != OpcodeToExtract || |
| 191 | !Op1 || Op1->getOpcode() != OpcodeToExtract) |
| 192 | return 0; |
| 193 | |
| 194 | // The expression has the form "(A op' B) op (C op' D)". |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 195 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1); |
| 196 | Value *C = Op1->getOperand(0), *D = Op1->getOperand(1); |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 197 | |
| 198 | // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)". |
| 199 | // Does the instruction have the form "(A op' B) op (A op' D)" or, in the |
| 200 | // commutative case, "(A op' B) op (C op' A)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 201 | if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) { |
| 202 | Value *DD = A == C ? D : C; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 203 | // Form "A op' (B op DD)" if it simplifies completely. |
| 204 | // Does "B op DD" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 205 | if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 206 | // It does! Return "A op' V" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 207 | // If V equals B then "A op' V" is just the LHS. If V equals DD then |
| 208 | // "A op' V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 209 | if (V == B || V == DD) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 210 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 211 | return V == B ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 212 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 213 | // Otherwise return "A op' V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 214 | if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, TLI, DT, |
| 215 | MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 216 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 217 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 218 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)". |
| 223 | // Does the instruction have the form "(A op' B) op (C op' B)" or, in the |
| 224 | // commutative case, "(A op' B) op (B op' D)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 225 | if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) { |
| 226 | Value *CC = B == D ? C : D; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 227 | // Form "(A op CC) op' B" if it simplifies completely.. |
| 228 | // Does "A op CC" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 229 | if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 230 | // It does! Return "V op' B" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 231 | // If V equals A then "V op' B" is just the LHS. If V equals CC then |
| 232 | // "V op' B" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 233 | if (V == A || V == CC) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 234 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 235 | return V == A ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 236 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 237 | // Otherwise return "V op' B" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 238 | if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, TLI, DT, |
| 239 | MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 240 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 241 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 242 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 250 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 251 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 252 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 253 | const TargetLibraryInfo *TLI, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 254 | const DominatorTree *DT, |
| 255 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 256 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 257 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 258 | |
| 259 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 260 | if (!MaxRecurse--) |
| 261 | return 0; |
| 262 | |
| 263 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 264 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 265 | |
| 266 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 267 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 268 | Value *A = Op0->getOperand(0); |
| 269 | Value *B = Op0->getOperand(1); |
| 270 | Value *C = RHS; |
| 271 | |
| 272 | // Does "B op C" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 273 | if (Value *V = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 274 | // It does! Return "A op V" if it simplifies or is already available. |
| 275 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 276 | if (V == B) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 277 | // Otherwise return "A op V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 278 | if (Value *W = SimplifyBinOp(Opcode, A, V, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 279 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 280 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 281 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 286 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 287 | Value *A = LHS; |
| 288 | Value *B = Op1->getOperand(0); |
| 289 | Value *C = Op1->getOperand(1); |
| 290 | |
| 291 | // Does "A op B" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 292 | if (Value *V = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 293 | // It does! Return "V op C" if it simplifies or is already available. |
| 294 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 295 | if (V == B) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 296 | // Otherwise return "V op C" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 297 | if (Value *W = SimplifyBinOp(Opcode, V, C, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 298 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 299 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 300 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | // The remaining transforms require commutativity as well as associativity. |
| 305 | if (!Instruction::isCommutative(Opcode)) |
| 306 | return 0; |
| 307 | |
| 308 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 309 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 310 | Value *A = Op0->getOperand(0); |
| 311 | Value *B = Op0->getOperand(1); |
| 312 | Value *C = RHS; |
| 313 | |
| 314 | // Does "C op A" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 315 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 316 | // It does! Return "V op B" if it simplifies or is already available. |
| 317 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 318 | if (V == A) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 319 | // Otherwise return "V op B" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 320 | if (Value *W = SimplifyBinOp(Opcode, V, B, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 321 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 322 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 323 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 328 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 329 | Value *A = LHS; |
| 330 | Value *B = Op1->getOperand(0); |
| 331 | Value *C = Op1->getOperand(1); |
| 332 | |
| 333 | // Does "C op A" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 334 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 335 | // It does! Return "B op V" if it simplifies or is already available. |
| 336 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 337 | if (V == C) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 338 | // Otherwise return "B op V" if it simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 339 | if (Value *W = SimplifyBinOp(Opcode, B, V, TD, TLI, DT, MaxRecurse)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 340 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 341 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 342 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 349 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 350 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 351 | /// evaluating it on both branches of the select results in the same value. |
| 352 | /// Returns the common value if so, otherwise returns null. |
| 353 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 354 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 355 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 356 | const DominatorTree *DT, |
| 357 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 358 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 359 | if (!MaxRecurse--) |
| 360 | return 0; |
| 361 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 362 | SelectInst *SI; |
| 363 | if (isa<SelectInst>(LHS)) { |
| 364 | SI = cast<SelectInst>(LHS); |
| 365 | } else { |
| 366 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 367 | SI = cast<SelectInst>(RHS); |
| 368 | } |
| 369 | |
| 370 | // Evaluate the BinOp on the true and false branches of the select. |
| 371 | Value *TV; |
| 372 | Value *FV; |
| 373 | if (SI == LHS) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 374 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, TLI, DT, MaxRecurse); |
| 375 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 376 | } else { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 377 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, TLI, DT, MaxRecurse); |
| 378 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, TLI, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Duncan Sands | 7cf85e7 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 381 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 382 | // If they both failed to simplify then return null. |
| 383 | if (TV == FV) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 384 | return TV; |
| 385 | |
| 386 | // If one branch simplified to undef, return the other one. |
| 387 | if (TV && isa<UndefValue>(TV)) |
| 388 | return FV; |
| 389 | if (FV && isa<UndefValue>(FV)) |
| 390 | return TV; |
| 391 | |
| 392 | // If applying the operation did not change the true and false select values, |
| 393 | // then the result of the binop is the select itself. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 394 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 395 | return SI; |
| 396 | |
| 397 | // If one branch simplified and the other did not, and the simplified |
| 398 | // value is equal to the unsimplified one, return the simplified value. |
| 399 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 400 | if ((FV && !TV) || (TV && !FV)) { |
| 401 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 402 | // same as the original operation. |
| 403 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 404 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 405 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 406 | // We already know that "op" is the same as for the simplified value. See |
| 407 | // if the operands match too. If so, return the simplified value. |
| 408 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 409 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 410 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 411 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 412 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 413 | return Simplified; |
| 414 | if (Simplified->isCommutative() && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 415 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 416 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 417 | return Simplified; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 425 | /// try to simplify the comparison by seeing whether both branches of the select |
| 426 | /// result in the same value. Returns the common value if so, otherwise returns |
| 427 | /// null. |
| 428 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 429 | Value *RHS, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 430 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 431 | const DominatorTree *DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 432 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 433 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 434 | if (!MaxRecurse--) |
| 435 | return 0; |
| 436 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 437 | // Make sure the select is on the LHS. |
| 438 | if (!isa<SelectInst>(LHS)) { |
| 439 | std::swap(LHS, RHS); |
| 440 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 441 | } |
| 442 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 443 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 444 | Value *Cond = SI->getCondition(); |
| 445 | Value *TV = SI->getTrueValue(); |
| 446 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 447 | |
Duncan Sands | 50ca4d3 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 448 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 449 | // Does "cmp TV, RHS" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 450 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 451 | if (TCmp == Cond) { |
| 452 | // It not only simplified, it simplified to the select condition. Replace |
| 453 | // it with 'true'. |
| 454 | TCmp = getTrue(Cond->getType()); |
| 455 | } else if (!TCmp) { |
| 456 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 457 | // condition then we can replace it with 'true'. Otherwise give up. |
| 458 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
| 459 | return 0; |
| 460 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 50ca4d3 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 463 | // Does "cmp FV, RHS" simplify? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 464 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 465 | if (FCmp == Cond) { |
| 466 | // It not only simplified, it simplified to the select condition. Replace |
| 467 | // it with 'false'. |
| 468 | FCmp = getFalse(Cond->getType()); |
| 469 | } else if (!FCmp) { |
| 470 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 471 | // condition then we can replace it with 'false'. Otherwise give up. |
| 472 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
| 473 | return 0; |
| 474 | FCmp = getFalse(Cond->getType()); |
| 475 | } |
| 476 | |
| 477 | // If both sides simplified to the same value, then use it as the result of |
| 478 | // the original comparison. |
| 479 | if (TCmp == FCmp) |
| 480 | return TCmp; |
Duncan Sands | aa97bb5 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 481 | |
| 482 | // The remaining cases only make sense if the select condition has the same |
| 483 | // type as the result of the comparison, so bail out if this is not so. |
| 484 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
| 485 | return 0; |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 486 | // If the false value simplified to false, then the result of the compare |
| 487 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 488 | // value simplified to false and the true value to true, returning "Cond". |
| 489 | if (match(FCmp, m_Zero())) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 490 | if (Value *V = SimplifyAndInst(Cond, TCmp, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 491 | return V; |
| 492 | // If the true value simplified to true, then the result of the compare |
| 493 | // is equal to "Cond || FCmp". |
| 494 | if (match(TCmp, m_One())) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 495 | if (Value *V = SimplifyOrInst(Cond, FCmp, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 496 | return V; |
| 497 | // Finally, if the false value simplified to true and the true value to |
| 498 | // false, then the result of the compare is equal to "!Cond". |
| 499 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 500 | if (Value *V = |
| 501 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 502 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 6dc9e2b | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 503 | return V; |
| 504 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 508 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 509 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 510 | /// it on the incoming phi values yields the same result for every value. If so |
| 511 | /// returns the common value, otherwise returns null. |
| 512 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 513 | const TargetData *TD, |
| 514 | const TargetLibraryInfo *TLI, |
| 515 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 516 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 517 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 518 | if (!MaxRecurse--) |
| 519 | return 0; |
| 520 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 521 | PHINode *PI; |
| 522 | if (isa<PHINode>(LHS)) { |
| 523 | PI = cast<PHINode>(LHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 524 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 525 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 526 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 527 | } else { |
| 528 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 529 | PI = cast<PHINode>(RHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 530 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
| 531 | if (!ValueDominatesPHI(LHS, PI, DT)) |
| 532 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Evaluate the BinOp on the incoming phi values. |
| 536 | Value *CommonValue = 0; |
| 537 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 538 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 539 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 540 | if (Incoming == PI) continue; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 541 | Value *V = PI == LHS ? |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 542 | SimplifyBinOp(Opcode, Incoming, RHS, TD, TLI, DT, MaxRecurse) : |
| 543 | SimplifyBinOp(Opcode, LHS, Incoming, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 544 | // If the operation failed to simplify, or simplified to a different value |
| 545 | // to previously, then give up. |
| 546 | if (!V || (CommonValue && V != CommonValue)) |
| 547 | return 0; |
| 548 | CommonValue = V; |
| 549 | } |
| 550 | |
| 551 | return CommonValue; |
| 552 | } |
| 553 | |
| 554 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 555 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 556 | /// incoming phi values yields the same result every time. If so returns the |
| 557 | /// common result, otherwise returns null. |
| 558 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 559 | const TargetData *TD, |
| 560 | const TargetLibraryInfo *TLI, |
| 561 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 562 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 563 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 564 | if (!MaxRecurse--) |
| 565 | return 0; |
| 566 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 567 | // Make sure the phi is on the LHS. |
| 568 | if (!isa<PHINode>(LHS)) { |
| 569 | std::swap(LHS, RHS); |
| 570 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 571 | } |
| 572 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 573 | PHINode *PI = cast<PHINode>(LHS); |
| 574 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 575 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 576 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 577 | return 0; |
| 578 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 579 | // Evaluate the BinOp on the incoming phi values. |
| 580 | Value *CommonValue = 0; |
| 581 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 582 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 583 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 584 | if (Incoming == PI) continue; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 585 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 586 | // If the operation failed to simplify, or simplified to a different value |
| 587 | // to previously, then give up. |
| 588 | if (!V || (CommonValue && V != CommonValue)) |
| 589 | return 0; |
| 590 | CommonValue = V; |
| 591 | } |
| 592 | |
| 593 | return CommonValue; |
| 594 | } |
| 595 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 596 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 597 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 598 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 599 | const TargetData *TD, |
| 600 | const TargetLibraryInfo *TLI, |
| 601 | const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 602 | unsigned MaxRecurse) { |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 603 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 604 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 605 | Constant *Ops[] = { CLHS, CRHS }; |
| 606 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 607 | Ops, TD, TLI); |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 608 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 610 | // Canonicalize the constant to the RHS. |
| 611 | std::swap(Op0, Op1); |
| 612 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 613 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 614 | // X + undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 615 | if (match(Op1, m_Undef())) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 616 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 617 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 618 | // X + 0 -> X |
| 619 | if (match(Op1, m_Zero())) |
| 620 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 621 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 622 | // X + (Y - X) -> Y |
| 623 | // (Y - X) + X -> Y |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 624 | // Eg: X + -X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 625 | Value *Y = 0; |
| 626 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 627 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 628 | return Y; |
| 629 | |
| 630 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 631 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 632 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 633 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 634 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 635 | /// i1 add -> xor. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 636 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 637 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 638 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 639 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 640 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 641 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 642 | MaxRecurse)) |
| 643 | return V; |
| 644 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 645 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 646 | if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 647 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 648 | return V; |
| 649 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 650 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 651 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 652 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 653 | // only if B and C are equal. If B and C are equal then (since we assume |
| 654 | // that operands have already been simplified) "select(cond, B, C)" should |
| 655 | // have been simplified to the common value of B and C already. Analysing |
| 656 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 657 | // for threading over phi nodes. |
| 658 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 662 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 663 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 664 | const DominatorTree *DT) { |
| 665 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 668 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 669 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 670 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 671 | const TargetData *TD, |
| 672 | const TargetLibraryInfo *TLI, |
| 673 | const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 674 | unsigned MaxRecurse) { |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 675 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 676 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 677 | Constant *Ops[] = { CLHS, CRHS }; |
| 678 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 679 | Ops, TD, TLI); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // X - undef -> undef |
| 683 | // undef - X -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 684 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 685 | return UndefValue::get(Op0->getType()); |
| 686 | |
| 687 | // X - 0 -> X |
| 688 | if (match(Op1, m_Zero())) |
| 689 | return Op0; |
| 690 | |
| 691 | // X - X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 692 | if (Op0 == Op1) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 693 | return Constant::getNullValue(Op0->getType()); |
| 694 | |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 695 | // (X*2) - X -> X |
| 696 | // (X<<1) - X -> X |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 697 | Value *X = 0; |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 698 | if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) || |
| 699 | match(Op0, m_Shl(m_Specific(Op1), m_One()))) |
| 700 | return Op1; |
| 701 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 702 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 703 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
| 704 | Value *Y = 0, *Z = Op1; |
| 705 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 706 | // See if "V === Y - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 707 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 708 | // It does! Now see if "X + V" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 709 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 710 | MaxRecurse-1)) { |
| 711 | // It does, we successfully reassociated! |
| 712 | ++NumReassoc; |
| 713 | return W; |
| 714 | } |
| 715 | // See if "V === X - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 716 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 717 | // It does! Now see if "Y + V" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 718 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 719 | MaxRecurse-1)) { |
| 720 | // It does, we successfully reassociated! |
| 721 | ++NumReassoc; |
| 722 | return W; |
| 723 | } |
| 724 | } |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 725 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 726 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 727 | // For example, X - (X + 1) -> -1 |
| 728 | X = Op0; |
| 729 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 730 | // See if "V === X - Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 731 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 732 | // It does! Now see if "V - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 733 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 734 | MaxRecurse-1)) { |
| 735 | // It does, we successfully reassociated! |
| 736 | ++NumReassoc; |
| 737 | return W; |
| 738 | } |
| 739 | // See if "V === X - Z" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 740 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 741 | // It does! Now see if "V - Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 742 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, TLI, DT, |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 743 | MaxRecurse-1)) { |
| 744 | // It does, we successfully reassociated! |
| 745 | ++NumReassoc; |
| 746 | return W; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 751 | // For example, X - (X - Y) -> Y. |
| 752 | Z = Op0; |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 753 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 754 | // See if "V === Z - X" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 755 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 756 | // It does! Now see if "V + Y" simplifies. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 757 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, TLI, DT, |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 758 | MaxRecurse-1)) { |
| 759 | // It does, we successfully reassociated! |
| 760 | ++NumReassoc; |
| 761 | return W; |
| 762 | } |
| 763 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 764 | // Mul distributes over Sub. Try some generic simplifications based on this. |
| 765 | if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 766 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 767 | return V; |
| 768 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 769 | // i1 sub -> xor. |
| 770 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 771 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 772 | return V; |
| 773 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 774 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 775 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 776 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 777 | // only if B and C are equal. If B and C are equal then (since we assume |
| 778 | // that operands have already been simplified) "select(cond, B, C)" should |
| 779 | // have been simplified to the common value of B and C already. Analysing |
| 780 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 781 | // for threading over phi nodes. |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 786 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 787 | const TargetData *TD, |
| 788 | const TargetLibraryInfo *TLI, |
| 789 | const DominatorTree *DT) { |
| 790 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 793 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 794 | /// fold the result. If not, this returns null. |
| 795 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 796 | const TargetLibraryInfo *TLI, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 797 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 798 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 799 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 800 | Constant *Ops[] = { CLHS, CRHS }; |
| 801 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 802 | Ops, TD, TLI); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | // Canonicalize the constant to the RHS. |
| 806 | std::swap(Op0, Op1); |
| 807 | } |
| 808 | |
| 809 | // X * undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 810 | if (match(Op1, m_Undef())) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 811 | return Constant::getNullValue(Op0->getType()); |
| 812 | |
| 813 | // X * 0 -> 0 |
| 814 | if (match(Op1, m_Zero())) |
| 815 | return Op1; |
| 816 | |
| 817 | // X * 1 -> X |
| 818 | if (match(Op1, m_One())) |
| 819 | return Op0; |
| 820 | |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 821 | // (X / Y) * Y -> X if the division is exact. |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 822 | Value *X = 0; |
| 823 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 824 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 825 | return X; |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 826 | |
Nick Lewycky | 5413880 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 827 | // i1 mul -> and. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 828 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 829 | if (Value *V = SimplifyAndInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 830 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 831 | |
| 832 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 833 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 834 | MaxRecurse)) |
| 835 | return V; |
| 836 | |
| 837 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 838 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 839 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 840 | return V; |
| 841 | |
| 842 | // If the operation is with the result of a select instruction, check whether |
| 843 | // operating on either branch of the select always yields the same value. |
| 844 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 845 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 846 | MaxRecurse)) |
| 847 | return V; |
| 848 | |
| 849 | // If the operation is with the result of a phi instruction, check whether |
| 850 | // operating on all incoming values of the phi always yields the same value. |
| 851 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 852 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 853 | MaxRecurse)) |
| 854 | return V; |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 860 | const TargetLibraryInfo *TLI, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 861 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 862 | return ::SimplifyMulInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 865 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 866 | /// fold the result. If not, this returns null. |
Anders Carlsson | 479b4b9 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 867 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 868 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 869 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 870 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 871 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 872 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 873 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 877 | bool isSigned = Opcode == Instruction::SDiv; |
| 878 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 879 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 880 | if (match(Op1, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 881 | return Op1; |
| 882 | |
| 883 | // undef / X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 884 | if (match(Op0, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 885 | return Constant::getNullValue(Op0->getType()); |
| 886 | |
| 887 | // 0 / X -> 0, we don't need to preserve faults! |
| 888 | if (match(Op0, m_Zero())) |
| 889 | return Op0; |
| 890 | |
| 891 | // X / 1 -> X |
| 892 | if (match(Op1, m_One())) |
| 893 | return Op0; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 894 | |
| 895 | if (Op0->getType()->isIntegerTy(1)) |
| 896 | // It can't be division by zero, hence it must be division by one. |
| 897 | return Op0; |
| 898 | |
| 899 | // X / X -> 1 |
| 900 | if (Op0 == Op1) |
| 901 | return ConstantInt::get(Op0->getType(), 1); |
| 902 | |
| 903 | // (X * Y) / Y -> X if the multiplication does not overflow. |
| 904 | Value *X = 0, *Y = 0; |
| 905 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 906 | if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 |
Duncan Sands | 32a43cc | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 907 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 4b72071 | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 908 | // If the Mul knows it does not overflow, then we are good to go. |
| 909 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 910 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 911 | return X; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 912 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 913 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 914 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 915 | return X; |
| 916 | } |
| 917 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 918 | // (X rem Y) / Y -> 0 |
| 919 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 920 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 921 | return Constant::getNullValue(Op0->getType()); |
| 922 | |
| 923 | // If the operation is with the result of a select instruction, check whether |
| 924 | // operating on either branch of the select always yields the same value. |
| 925 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 926 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, |
| 927 | MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 928 | return V; |
| 929 | |
| 930 | // If the operation is with the result of a phi instruction, check whether |
| 931 | // operating on all incoming values of the phi always yields the same value. |
| 932 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 933 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, |
| 934 | MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 935 | return V; |
| 936 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 941 | /// fold the result. If not, this returns null. |
| 942 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 943 | const TargetLibraryInfo *TLI, |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 944 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 945 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, TLI, DT, |
| 946 | MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 947 | return V; |
| 948 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 953 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 954 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 955 | return ::SimplifySDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 959 | /// fold the result. If not, this returns null. |
| 960 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 961 | const TargetLibraryInfo *TLI, |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 962 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 963 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, TLI, DT, |
| 964 | MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 965 | return V; |
| 966 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 971 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 972 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 973 | return ::SimplifyUDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 976 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 977 | const TargetLibraryInfo *, |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 978 | const DominatorTree *, unsigned) { |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 979 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 980 | if (match(Op0, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 981 | return Op0; |
| 982 | |
| 983 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 984 | if (match(Op1, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 985 | return Op1; |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 991 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 992 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 993 | return ::SimplifyFDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 996 | /// SimplifyRem - Given operands for an SRem or URem, see if we can |
| 997 | /// fold the result. If not, this returns null. |
| 998 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 999 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 1000 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1001 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1002 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1003 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1004 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1008 | // X % undef -> undef |
| 1009 | if (match(Op1, m_Undef())) |
| 1010 | return Op1; |
| 1011 | |
| 1012 | // undef % X -> 0 |
| 1013 | if (match(Op0, m_Undef())) |
| 1014 | return Constant::getNullValue(Op0->getType()); |
| 1015 | |
| 1016 | // 0 % X -> 0, we don't need to preserve faults! |
| 1017 | if (match(Op0, m_Zero())) |
| 1018 | return Op0; |
| 1019 | |
| 1020 | // X % 0 -> undef, we don't need to preserve faults! |
| 1021 | if (match(Op1, m_Zero())) |
| 1022 | return UndefValue::get(Op0->getType()); |
| 1023 | |
| 1024 | // X % 1 -> 0 |
| 1025 | if (match(Op1, m_One())) |
| 1026 | return Constant::getNullValue(Op0->getType()); |
| 1027 | |
| 1028 | if (Op0->getType()->isIntegerTy(1)) |
| 1029 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1030 | return Constant::getNullValue(Op0->getType()); |
| 1031 | |
| 1032 | // X % X -> 0 |
| 1033 | if (Op0 == Op1) |
| 1034 | return Constant::getNullValue(Op0->getType()); |
| 1035 | |
| 1036 | // If the operation is with the result of a select instruction, check whether |
| 1037 | // operating on either branch of the select always yields the same value. |
| 1038 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1039 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1040 | return V; |
| 1041 | |
| 1042 | // If the operation is with the result of a phi instruction, check whether |
| 1043 | // operating on all incoming values of the phi always yields the same value. |
| 1044 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1045 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1046 | return V; |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | /// SimplifySRemInst - Given operands for an SRem, see if we can |
| 1052 | /// fold the result. If not, this returns null. |
| 1053 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1054 | const TargetLibraryInfo *TLI, |
| 1055 | const DominatorTree *DT, |
| 1056 | unsigned MaxRecurse) { |
| 1057 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1058 | return V; |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1064 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1065 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1066 | return ::SimplifySRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /// SimplifyURemInst - Given operands for a URem, see if we can |
| 1070 | /// fold the result. If not, this returns null. |
| 1071 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1072 | const TargetLibraryInfo *TLI, |
| 1073 | const DominatorTree *DT, |
| 1074 | unsigned MaxRecurse) { |
| 1075 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1076 | return V; |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1082 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1083 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1084 | return ::SimplifyURemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1088 | const TargetLibraryInfo *, |
| 1089 | const DominatorTree *, |
| 1090 | unsigned) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1091 | // undef % X -> undef (the undef could be a snan). |
| 1092 | if (match(Op0, m_Undef())) |
| 1093 | return Op0; |
| 1094 | |
| 1095 | // X % undef -> undef |
| 1096 | if (match(Op1, m_Undef())) |
| 1097 | return Op1; |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1103 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1104 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1105 | return ::SimplifyFRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1108 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1109 | /// fold the result. If not, this returns null. |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1110 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1111 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 1112 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1113 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1114 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1115 | Constant *Ops[] = { C0, C1 }; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1116 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1120 | // 0 shift by X -> 0 |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1121 | if (match(Op0, m_Zero())) |
| 1122 | return Op0; |
| 1123 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1124 | // X shift by 0 -> X |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1125 | if (match(Op1, m_Zero())) |
| 1126 | return Op0; |
| 1127 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1128 | // X shift by undef -> undef because it may shift by the bitwidth. |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1129 | if (match(Op1, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1130 | return Op1; |
| 1131 | |
| 1132 | // Shifting by the bitwidth or more is undefined. |
| 1133 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) |
| 1134 | if (CI->getValue().getLimitedValue() >= |
| 1135 | Op0->getType()->getScalarSizeInBits()) |
| 1136 | return UndefValue::get(Op0->getType()); |
| 1137 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1138 | // If the operation is with the result of a select instruction, check whether |
| 1139 | // operating on either branch of the select always yields the same value. |
| 1140 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1141 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1142 | return V; |
| 1143 | |
| 1144 | // If the operation is with the result of a phi instruction, check whether |
| 1145 | // operating on all incoming values of the phi always yields the same value. |
| 1146 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1147 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1148 | return V; |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 1154 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1155 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1156 | const TargetData *TD, |
| 1157 | const TargetLibraryInfo *TLI, |
| 1158 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1159 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1160 | return V; |
| 1161 | |
| 1162 | // undef << X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1163 | if (match(Op0, m_Undef())) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1164 | return Constant::getNullValue(Op0->getType()); |
| 1165 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1166 | // (X >> A) << A -> X |
| 1167 | Value *X; |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1168 | if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))) |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1169 | return X; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1173 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1174 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 1175 | const DominatorTree *DT) { |
| 1176 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 1180 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1181 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1182 | const TargetData *TD, |
| 1183 | const TargetLibraryInfo *TLI, |
| 1184 | const DominatorTree *DT, |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1185 | unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1186 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1187 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1188 | |
| 1189 | // undef >>l X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1190 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1191 | return Constant::getNullValue(Op0->getType()); |
| 1192 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1193 | // (X << A) >> A -> X |
| 1194 | Value *X; |
| 1195 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1196 | cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap()) |
| 1197 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1198 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1202 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1203 | const TargetData *TD, |
| 1204 | const TargetLibraryInfo *TLI, |
| 1205 | const DominatorTree *DT) { |
| 1206 | return ::SimplifyLShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 1210 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1211 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1212 | const TargetData *TD, |
| 1213 | const TargetLibraryInfo *TLI, |
| 1214 | const DominatorTree *DT, |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1215 | unsigned MaxRecurse) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1216 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1217 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1218 | |
| 1219 | // all ones >>a X -> all ones |
| 1220 | if (match(Op0, m_AllOnes())) |
| 1221 | return Op0; |
| 1222 | |
| 1223 | // undef >>a X -> all ones |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1224 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1225 | return Constant::getAllOnesValue(Op0->getType()); |
| 1226 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1227 | // (X << A) >> A -> X |
| 1228 | Value *X; |
| 1229 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1230 | cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap()) |
| 1231 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1232 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1233 | return 0; |
| 1234 | } |
| 1235 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1236 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1237 | const TargetData *TD, |
| 1238 | const TargetLibraryInfo *TLI, |
| 1239 | const DominatorTree *DT) { |
| 1240 | return ::SimplifyAShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1243 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1244 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1245 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1246 | const TargetLibraryInfo *TLI, |
| 1247 | const DominatorTree *DT, |
| 1248 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1249 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1250 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1251 | Constant *Ops[] = { CLHS, CRHS }; |
| 1252 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1253 | Ops, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1254 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1256 | // Canonicalize the constant to the RHS. |
| 1257 | std::swap(Op0, Op1); |
| 1258 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1260 | // X & undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1261 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1262 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1263 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1264 | // X & X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1265 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1266 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1267 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1268 | // X & 0 = 0 |
| 1269 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1270 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1271 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1272 | // X & -1 = X |
| 1273 | if (match(Op1, m_AllOnes())) |
| 1274 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1276 | // A & ~A = ~A & A = 0 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1277 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1278 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1279 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1281 | // (A | ?) & A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1282 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1283 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1284 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1285 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1287 | // A & (A | ?) = A |
| 1288 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1289 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1290 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1291 | |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1292 | // A & (-A) = A if A is a power of two or zero. |
| 1293 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1294 | match(Op1, m_Neg(m_Specific(Op0)))) { |
| 1295 | if (isPowerOfTwo(Op0, TD, /*OrZero*/true)) |
| 1296 | return Op0; |
| 1297 | if (isPowerOfTwo(Op1, TD, /*OrZero*/true)) |
| 1298 | return Op1; |
| 1299 | } |
| 1300 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1301 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1302 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, TLI, |
| 1303 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1304 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1305 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1306 | // And distributes over Or. Try some generic simplifications based on this. |
| 1307 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1308 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1309 | return V; |
| 1310 | |
| 1311 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1312 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1313 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1314 | return V; |
| 1315 | |
| 1316 | // Or distributes over And. Try some generic simplifications based on this. |
| 1317 | if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1318 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1319 | return V; |
| 1320 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1321 | // If the operation is with the result of a select instruction, check whether |
| 1322 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1323 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1324 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, TLI, |
| 1325 | DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1326 | return V; |
| 1327 | |
| 1328 | // If the operation is with the result of a phi instruction, check whether |
| 1329 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1330 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1331 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1332 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1333 | return V; |
| 1334 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |
| 1337 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1338 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1339 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1340 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1341 | return ::SimplifyAndInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1344 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1345 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1346 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1347 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1348 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1349 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1350 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1351 | Constant *Ops[] = { CLHS, CRHS }; |
| 1352 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1353 | Ops, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1354 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1355 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1356 | // Canonicalize the constant to the RHS. |
| 1357 | std::swap(Op0, Op1); |
| 1358 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1360 | // X | undef -> -1 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1361 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1362 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1364 | // X | X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1365 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1366 | return Op0; |
| 1367 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1368 | // X | 0 = X |
| 1369 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1370 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1371 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1372 | // X | -1 = -1 |
| 1373 | if (match(Op1, m_AllOnes())) |
| 1374 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1376 | // A | ~A = ~A | A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1377 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1378 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1379 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1381 | // (A & ?) | A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1382 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1383 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1384 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1385 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1387 | // A | (A & ?) = A |
| 1388 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1389 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1390 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1391 | |
Benjamin Kramer | 38f7f66 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1392 | // ~(A & ?) | A = -1 |
| 1393 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1394 | (A == Op1 || B == Op1)) |
| 1395 | return Constant::getAllOnesValue(Op1->getType()); |
| 1396 | |
| 1397 | // A | ~(A & ?) = -1 |
| 1398 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1399 | (A == Op0 || B == Op0)) |
| 1400 | return Constant::getAllOnesValue(Op0->getType()); |
| 1401 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1402 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1403 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, TLI, |
| 1404 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1405 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1406 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1407 | // Or distributes over And. Try some generic simplifications based on this. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1408 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, TD, |
| 1409 | TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1410 | return V; |
| 1411 | |
| 1412 | // And distributes over Or. Try some generic simplifications based on this. |
| 1413 | if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1414 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1415 | return V; |
| 1416 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1417 | // If the operation is with the result of a select instruction, check whether |
| 1418 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1419 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1420 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1421 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1422 | return V; |
| 1423 | |
| 1424 | // If the operation is with the result of a phi instruction, check whether |
| 1425 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1426 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1427 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1428 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1429 | return V; |
| 1430 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
| 1433 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1434 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1435 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1436 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1437 | return ::SimplifyOrInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1438 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1439 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1440 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1441 | /// fold the result. If not, this returns null. |
| 1442 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1443 | const TargetLibraryInfo *TLI, |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1444 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1445 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1446 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1447 | Constant *Ops[] = { CLHS, CRHS }; |
| 1448 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1449 | Ops, TD, TLI); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | // Canonicalize the constant to the RHS. |
| 1453 | std::swap(Op0, Op1); |
| 1454 | } |
| 1455 | |
| 1456 | // A ^ undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1457 | if (match(Op1, m_Undef())) |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1458 | return Op1; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1459 | |
| 1460 | // A ^ 0 = A |
| 1461 | if (match(Op1, m_Zero())) |
| 1462 | return Op0; |
| 1463 | |
Eli Friedman | f23d4ad | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1464 | // A ^ A = 0 |
| 1465 | if (Op0 == Op1) |
| 1466 | return Constant::getNullValue(Op0->getType()); |
| 1467 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1468 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1469 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1470 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1471 | return Constant::getAllOnesValue(Op0->getType()); |
| 1472 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1473 | // Try some generic simplifications for associative operations. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1474 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, TLI, |
| 1475 | DT, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1476 | return V; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1477 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1478 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1479 | if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1480 | TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1481 | return V; |
| 1482 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1483 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1484 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1485 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1486 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1487 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1488 | // have been simplified to the common value of B and C already. Analysing |
| 1489 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1490 | // for threading over phi nodes. |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1496 | const TargetLibraryInfo *TLI, |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1497 | const DominatorTree *DT) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1498 | return ::SimplifyXorInst(Op0, Op1, TD, TLI, DT, RecursionLimit); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1501 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1502 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1503 | } |
| 1504 | |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1505 | /// ExtractEquivalentCondition - Rummage around inside V looking for something |
| 1506 | /// equivalent to the comparison "LHS Pred RHS". Return such a value if found, |
| 1507 | /// otherwise return null. Helper function for analyzing max/min idioms. |
| 1508 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1509 | Value *LHS, Value *RHS) { |
| 1510 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1511 | if (!SI) |
| 1512 | return 0; |
| 1513 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1514 | if (!Cmp) |
| 1515 | return 0; |
| 1516 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1517 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1518 | return Cmp; |
| 1519 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1520 | LHS == CmpRHS && RHS == CmpLHS) |
| 1521 | return Cmp; |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
Chris Lattner | 009e265 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1526 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1527 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1528 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1529 | const TargetData *TD, |
| 1530 | const TargetLibraryInfo *TLI, |
| 1531 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1532 | unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1533 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1534 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1536 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1537 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1538 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1539 | |
| 1540 | // If we have a constant, make sure it is on the RHS. |
| 1541 | std::swap(LHS, RHS); |
| 1542 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1543 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1545 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 1546 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1548 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1549 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1550 | // because X could be 0. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1551 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1552 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1553 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1554 | // Special case logic when the operands have i1 type. |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 1555 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1556 | switch (Pred) { |
| 1557 | default: break; |
| 1558 | case ICmpInst::ICMP_EQ: |
| 1559 | // X == 1 -> X |
| 1560 | if (match(RHS, m_One())) |
| 1561 | return LHS; |
| 1562 | break; |
| 1563 | case ICmpInst::ICMP_NE: |
| 1564 | // X != 0 -> X |
| 1565 | if (match(RHS, m_Zero())) |
| 1566 | return LHS; |
| 1567 | break; |
| 1568 | case ICmpInst::ICMP_UGT: |
| 1569 | // X >u 0 -> X |
| 1570 | if (match(RHS, m_Zero())) |
| 1571 | return LHS; |
| 1572 | break; |
| 1573 | case ICmpInst::ICMP_UGE: |
| 1574 | // X >=u 1 -> X |
| 1575 | if (match(RHS, m_One())) |
| 1576 | return LHS; |
| 1577 | break; |
| 1578 | case ICmpInst::ICMP_SLT: |
| 1579 | // X <s 0 -> X |
| 1580 | if (match(RHS, m_Zero())) |
| 1581 | return LHS; |
| 1582 | break; |
| 1583 | case ICmpInst::ICMP_SLE: |
| 1584 | // X <=s -1 -> X |
| 1585 | if (match(RHS, m_One())) |
| 1586 | return LHS; |
| 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1591 | // icmp <object*>, <object*/null> - Different identified objects have |
| 1592 | // different addresses (unless null), and what's more the address of an |
| 1593 | // identified local is never equal to another argument (again, barring null). |
| 1594 | // Note that generalizing to the case where LHS is a global variable address |
| 1595 | // or null is pointless, since if both LHS and RHS are constants then we |
| 1596 | // already constant folded the compare, and if only one of them is then we |
| 1597 | // moved it to RHS already. |
Benjamin Kramer | ea79b8e | 2012-02-16 15:19:59 +0000 | [diff] [blame] | 1598 | Value *LHSPtr = LHS->stripPointerCasts(); |
| 1599 | Value *RHSPtr = RHS->stripPointerCasts(); |
Eli Friedman | 2c3acb0 | 2012-02-18 03:29:25 +0000 | [diff] [blame] | 1600 | if (LHSPtr == RHSPtr) |
| 1601 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | b053fc1 | 2012-02-20 00:42:49 +0000 | [diff] [blame] | 1603 | // Be more aggressive about stripping pointer adjustments when checking a |
| 1604 | // comparison of an alloca address to another object. We can rip off all |
| 1605 | // inbounds GEP operations, even if they are variable. |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 1606 | LHSPtr = LHSPtr->stripInBoundsOffsets(); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1607 | if (llvm::isIdentifiedObject(LHSPtr)) { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 1608 | RHSPtr = RHSPtr->stripInBoundsOffsets(); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1609 | if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) { |
| 1610 | // If both sides are different identified objects, they aren't equal |
| 1611 | // unless they're null. |
Bill Wendling | c17731d65 | 2012-03-10 17:56:03 +0000 | [diff] [blame^] | 1612 | if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr) && |
| 1613 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ)) |
| 1614 | return ConstantInt::get(ITy, false); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1615 | |
| 1616 | // A local identified object (alloca or noalias call) can't equal any |
| 1617 | // incoming argument, unless they're both null. |
Bill Wendling | c17731d65 | 2012-03-10 17:56:03 +0000 | [diff] [blame^] | 1618 | if (isa<Instruction>(LHSPtr) && isa<Argument>(RHSPtr) && |
| 1619 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ)) |
| 1620 | return ConstantInt::get(ITy, false); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | // Assume that the constant null is on the right. |
Bill Wendling | c17731d65 | 2012-03-10 17:56:03 +0000 | [diff] [blame^] | 1624 | if (llvm::isKnownNonNull(LHSPtr) && isa<ConstantPointerNull>(RHSPtr)) { |
| 1625 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ) |
| 1626 | return ConstantInt::get(ITy, false); |
| 1627 | else if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::FCMP_ONE) |
| 1628 | return ConstantInt::get(ITy, true); |
| 1629 | } |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1630 | } else if (isa<Argument>(LHSPtr)) { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 1631 | RHSPtr = RHSPtr->stripInBoundsOffsets(); |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 1632 | // An alloca can't be equal to an argument. |
Bill Wendling | c17731d65 | 2012-03-10 17:56:03 +0000 | [diff] [blame^] | 1633 | if (isa<AllocaInst>(RHSPtr)) { |
| 1634 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::FCMP_UEQ) |
| 1635 | return ConstantInt::get(ITy, false); |
| 1636 | else if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::FCMP_ONE) |
| 1637 | return ConstantInt::get(ITy, true); |
| 1638 | } |
Chris Lattner | b053fc1 | 2012-02-20 00:42:49 +0000 | [diff] [blame] | 1639 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1640 | |
| 1641 | // If we are comparing with zero then try hard since this is a common case. |
| 1642 | if (match(RHS, m_Zero())) { |
| 1643 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 1644 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1645 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1646 | case ICmpInst::ICMP_ULT: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1647 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1648 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1649 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1650 | case ICmpInst::ICMP_EQ: |
| 1651 | case ICmpInst::ICMP_ULE: |
| 1652 | if (isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1653 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | case ICmpInst::ICMP_NE: |
| 1656 | case ICmpInst::ICMP_UGT: |
| 1657 | if (isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1658 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | case ICmpInst::ICMP_SLT: |
| 1661 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1662 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1663 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1664 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1665 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | case ICmpInst::ICMP_SLE: |
| 1668 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1669 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1670 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1671 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1672 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1673 | break; |
| 1674 | case ICmpInst::ICMP_SGE: |
| 1675 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1676 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1677 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1678 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1679 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1680 | break; |
| 1681 | case ICmpInst::ICMP_SGT: |
| 1682 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1683 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1684 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1685 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1686 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1692 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1693 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
| 1694 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue()); |
| 1695 | if (RHS_CR.isEmptySet()) |
| 1696 | return ConstantInt::getFalse(CI->getContext()); |
| 1697 | if (RHS_CR.isFullSet()) |
| 1698 | return ConstantInt::getTrue(CI->getContext()); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1699 | |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1700 | // Many binary operators with constant RHS have easy to compute constant |
| 1701 | // range. Use them to check whether the comparison is a tautology. |
| 1702 | uint32_t Width = CI->getBitWidth(); |
| 1703 | APInt Lower = APInt(Width, 0); |
| 1704 | APInt Upper = APInt(Width, 0); |
| 1705 | ConstantInt *CI2; |
| 1706 | if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) { |
| 1707 | // 'urem x, CI2' produces [0, CI2). |
| 1708 | Upper = CI2->getValue(); |
| 1709 | } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) { |
| 1710 | // 'srem x, CI2' produces (-|CI2|, |CI2|). |
| 1711 | Upper = CI2->getValue().abs(); |
| 1712 | Lower = (-Upper) + 1; |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 1713 | } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) { |
| 1714 | // 'udiv CI2, x' produces [0, CI2]. |
Eli Friedman | 7781ae5 | 2011-11-08 21:08:02 +0000 | [diff] [blame] | 1715 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1716 | } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1717 | // 'udiv x, CI2' produces [0, UINT_MAX / CI2]. |
| 1718 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1719 | if (!CI2->isZero()) |
| 1720 | Upper = NegOne.udiv(CI2->getValue()) + 1; |
| 1721 | } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1722 | // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2]. |
| 1723 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1724 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 1725 | APInt Val = CI2->getValue().abs(); |
| 1726 | if (!Val.isMinValue()) { |
| 1727 | Lower = IntMin.sdiv(Val); |
| 1728 | Upper = IntMax.sdiv(Val) + 1; |
| 1729 | } |
| 1730 | } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) { |
| 1731 | // 'lshr x, CI2' produces [0, UINT_MAX >> CI2]. |
| 1732 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1733 | if (CI2->getValue().ult(Width)) |
| 1734 | Upper = NegOne.lshr(CI2->getValue()) + 1; |
| 1735 | } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) { |
| 1736 | // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2]. |
| 1737 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1738 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 1739 | if (CI2->getValue().ult(Width)) { |
| 1740 | Lower = IntMin.ashr(CI2->getValue()); |
| 1741 | Upper = IntMax.ashr(CI2->getValue()) + 1; |
| 1742 | } |
| 1743 | } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) { |
| 1744 | // 'or x, CI2' produces [CI2, UINT_MAX]. |
| 1745 | Lower = CI2->getValue(); |
| 1746 | } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) { |
| 1747 | // 'and x, CI2' produces [0, CI2]. |
| 1748 | Upper = CI2->getValue() + 1; |
| 1749 | } |
| 1750 | if (Lower != Upper) { |
| 1751 | ConstantRange LHS_CR = ConstantRange(Lower, Upper); |
| 1752 | if (RHS_CR.contains(LHS_CR)) |
| 1753 | return ConstantInt::getTrue(RHS->getContext()); |
| 1754 | if (RHS_CR.inverse().contains(LHS_CR)) |
| 1755 | return ConstantInt::getFalse(RHS->getContext()); |
| 1756 | } |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1759 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 1760 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 1761 | Instruction *LI = cast<CastInst>(LHS); |
| 1762 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1763 | Type *SrcTy = SrcOp->getType(); |
| 1764 | Type *DstTy = LI->getType(); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 1767 | // if the integer type is the same size as the pointer type. |
| 1768 | if (MaxRecurse && TD && isa<PtrToIntInst>(LI) && |
| 1769 | TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) { |
| 1770 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1771 | // Transfer the cast to the constant. |
| 1772 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 1773 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1774 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1775 | return V; |
| 1776 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 1777 | if (RI->getOperand(0)->getType() == SrcTy) |
| 1778 | // Compare without the cast. |
| 1779 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1780 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1781 | return V; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | if (isa<ZExtInst>(LHS)) { |
| 1786 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 1787 | // same type. |
| 1788 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 1789 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1790 | // Compare X and Y. Note that signed predicates become unsigned. |
| 1791 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1792 | SrcOp, RI->getOperand(0), TD, TLI, DT, |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1793 | MaxRecurse-1)) |
| 1794 | return V; |
| 1795 | } |
| 1796 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 1797 | // too. If not, then try to deduce the result of the comparison. |
| 1798 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1799 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1800 | // reextended to DstTy. |
| 1801 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1802 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 1803 | |
| 1804 | // If the re-extended constant didn't change then this is effectively |
| 1805 | // also a case of comparing two zero-extended values. |
| 1806 | if (RExt == CI && MaxRecurse) |
| 1807 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1808 | SrcOp, Trunc, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1809 | return V; |
| 1810 | |
| 1811 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 1812 | // there. Use this to work out the result of the comparison. |
| 1813 | if (RExt != CI) { |
| 1814 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1815 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1816 | // LHS <u RHS. |
| 1817 | case ICmpInst::ICMP_EQ: |
| 1818 | case ICmpInst::ICMP_UGT: |
| 1819 | case ICmpInst::ICMP_UGE: |
| 1820 | return ConstantInt::getFalse(CI->getContext()); |
| 1821 | |
| 1822 | case ICmpInst::ICMP_NE: |
| 1823 | case ICmpInst::ICMP_ULT: |
| 1824 | case ICmpInst::ICMP_ULE: |
| 1825 | return ConstantInt::getTrue(CI->getContext()); |
| 1826 | |
| 1827 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 1828 | // is non-negative then LHS <s RHS. |
| 1829 | case ICmpInst::ICMP_SGT: |
| 1830 | case ICmpInst::ICMP_SGE: |
| 1831 | return CI->getValue().isNegative() ? |
| 1832 | ConstantInt::getTrue(CI->getContext()) : |
| 1833 | ConstantInt::getFalse(CI->getContext()); |
| 1834 | |
| 1835 | case ICmpInst::ICMP_SLT: |
| 1836 | case ICmpInst::ICMP_SLE: |
| 1837 | return CI->getValue().isNegative() ? |
| 1838 | ConstantInt::getFalse(CI->getContext()) : |
| 1839 | ConstantInt::getTrue(CI->getContext()); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | if (isa<SExtInst>(LHS)) { |
| 1846 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 1847 | // same type. |
| 1848 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 1849 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1850 | // Compare X and Y. Note that the predicate does not change. |
| 1851 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1852 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1853 | return V; |
| 1854 | } |
| 1855 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 1856 | // too. If not, then try to deduce the result of the comparison. |
| 1857 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1858 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1859 | // reextended to DstTy. |
| 1860 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1861 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 1862 | |
| 1863 | // If the re-extended constant didn't change then this is effectively |
| 1864 | // also a case of comparing two sign-extended values. |
| 1865 | if (RExt == CI && MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1866 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, TLI, DT, |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1867 | MaxRecurse-1)) |
| 1868 | return V; |
| 1869 | |
| 1870 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 1871 | // bits there. Use this to work out the result of the comparison. |
| 1872 | if (RExt != CI) { |
| 1873 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1874 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1875 | case ICmpInst::ICMP_EQ: |
| 1876 | return ConstantInt::getFalse(CI->getContext()); |
| 1877 | case ICmpInst::ICMP_NE: |
| 1878 | return ConstantInt::getTrue(CI->getContext()); |
| 1879 | |
| 1880 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 1881 | // LHS >s RHS. |
| 1882 | case ICmpInst::ICMP_SGT: |
| 1883 | case ICmpInst::ICMP_SGE: |
| 1884 | return CI->getValue().isNegative() ? |
| 1885 | ConstantInt::getTrue(CI->getContext()) : |
| 1886 | ConstantInt::getFalse(CI->getContext()); |
| 1887 | case ICmpInst::ICMP_SLT: |
| 1888 | case ICmpInst::ICMP_SLE: |
| 1889 | return CI->getValue().isNegative() ? |
| 1890 | ConstantInt::getFalse(CI->getContext()) : |
| 1891 | ConstantInt::getTrue(CI->getContext()); |
| 1892 | |
| 1893 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 1894 | // LHS >u RHS. |
| 1895 | case ICmpInst::ICMP_UGT: |
| 1896 | case ICmpInst::ICMP_UGE: |
| 1897 | // Comparison is true iff the LHS <s 0. |
| 1898 | if (MaxRecurse) |
| 1899 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 1900 | Constant::getNullValue(SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1901 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1902 | return V; |
| 1903 | break; |
| 1904 | case ICmpInst::ICMP_ULT: |
| 1905 | case ICmpInst::ICMP_ULE: |
| 1906 | // Comparison is true iff the LHS >=s 0. |
| 1907 | if (MaxRecurse) |
| 1908 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 1909 | Constant::getNullValue(SrcTy), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1910 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1911 | return V; |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1919 | // Special logic for binary operators. |
| 1920 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 1921 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 1922 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1923 | // Analyze the case when either LHS or RHS is an add instruction. |
| 1924 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 1925 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 1926 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 1927 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 1928 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 1929 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 1930 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 1931 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 1932 | } |
| 1933 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 1934 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 1935 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 1936 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 1937 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 1938 | } |
| 1939 | |
| 1940 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 1941 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 1942 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 1943 | Constant::getNullValue(RHS->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1944 | TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1945 | return V; |
| 1946 | |
| 1947 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 1948 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 1949 | if (Value *V = SimplifyICmpInst(Pred, |
| 1950 | Constant::getNullValue(LHS->getType()), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1951 | C == LHS ? D : C, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1952 | return V; |
| 1953 | |
| 1954 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 1955 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 1956 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 1957 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 1958 | Value *Y = (A == C || A == D) ? B : A; |
| 1959 | Value *Z = (C == A || C == B) ? D : C; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1960 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1961 | return V; |
| 1962 | } |
| 1963 | } |
| 1964 | |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1965 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1966 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1967 | switch (Pred) { |
| 1968 | default: |
| 1969 | break; |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1970 | case ICmpInst::ICMP_SGT: |
| 1971 | case ICmpInst::ICMP_SGE: |
| 1972 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD); |
| 1973 | if (!KnownNonNegative) |
| 1974 | break; |
| 1975 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1976 | case ICmpInst::ICMP_EQ: |
| 1977 | case ICmpInst::ICMP_UGT: |
| 1978 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1979 | return getFalse(ITy); |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 1980 | case ICmpInst::ICMP_SLT: |
| 1981 | case ICmpInst::ICMP_SLE: |
| 1982 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD); |
| 1983 | if (!KnownNonNegative) |
| 1984 | break; |
| 1985 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1986 | case ICmpInst::ICMP_NE: |
| 1987 | case ICmpInst::ICMP_ULT: |
| 1988 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1989 | return getTrue(ITy); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 1992 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 1993 | bool KnownNonNegative, KnownNegative; |
| 1994 | switch (Pred) { |
| 1995 | default: |
| 1996 | break; |
| 1997 | case ICmpInst::ICMP_SGT: |
| 1998 | case ICmpInst::ICMP_SGE: |
| 1999 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD); |
| 2000 | if (!KnownNonNegative) |
| 2001 | break; |
| 2002 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2003 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2004 | case ICmpInst::ICMP_UGT: |
| 2005 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2006 | return getTrue(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2007 | case ICmpInst::ICMP_SLT: |
| 2008 | case ICmpInst::ICMP_SLE: |
| 2009 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD); |
| 2010 | if (!KnownNonNegative) |
| 2011 | break; |
| 2012 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2013 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2014 | case ICmpInst::ICMP_ULT: |
| 2015 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2016 | return getFalse(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2019 | |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2020 | // x udiv y <=u x. |
| 2021 | if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { |
| 2022 | // icmp pred (X /u Y), X |
| 2023 | if (Pred == ICmpInst::ICMP_UGT) |
| 2024 | return getFalse(ITy); |
| 2025 | if (Pred == ICmpInst::ICMP_ULE) |
| 2026 | return getTrue(ITy); |
| 2027 | } |
| 2028 | |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2029 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2030 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2031 | switch (LBO->getOpcode()) { |
| 2032 | default: break; |
| 2033 | case Instruction::UDiv: |
| 2034 | case Instruction::LShr: |
| 2035 | if (ICmpInst::isSigned(Pred)) |
| 2036 | break; |
| 2037 | // fall-through |
| 2038 | case Instruction::SDiv: |
| 2039 | case Instruction::AShr: |
Eli Friedman | b6e7cd6 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2040 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2041 | break; |
| 2042 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2043 | RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2044 | return V; |
| 2045 | break; |
| 2046 | case Instruction::Shl: { |
Duncan Sands | c9d904e | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2047 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2048 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2049 | if (!NUW && !NSW) |
| 2050 | break; |
| 2051 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2052 | break; |
| 2053 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2054 | RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2055 | return V; |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
| 2059 | } |
| 2060 | |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2061 | // Simplify comparisons involving max/min. |
| 2062 | Value *A, *B; |
| 2063 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2064 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2065 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2066 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2067 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2068 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
| 2069 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2070 | // We analyze this as smax(A, B) pred A. |
| 2071 | P = Pred; |
| 2072 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2073 | (A == LHS || B == LHS)) { |
| 2074 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
| 2075 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2076 | // We analyze this as smax(A, B) swapped-pred A. |
| 2077 | P = CmpInst::getSwappedPredicate(Pred); |
| 2078 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2079 | (A == RHS || B == RHS)) { |
| 2080 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
| 2081 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2082 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2083 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2084 | P = CmpInst::getSwappedPredicate(Pred); |
| 2085 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2086 | (A == LHS || B == LHS)) { |
| 2087 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
| 2088 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2089 | // We analyze this as smax(-A, -B) pred -A. |
| 2090 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2091 | P = Pred; |
| 2092 | } |
| 2093 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2094 | // Cases correspond to "max(A, B) p A". |
| 2095 | switch (P) { |
| 2096 | default: |
| 2097 | break; |
| 2098 | case CmpInst::ICMP_EQ: |
| 2099 | case CmpInst::ICMP_SLE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2100 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2101 | // in the max/min; if so, we can just return that. |
| 2102 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2103 | return V; |
| 2104 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2105 | return V; |
| 2106 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2107 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2108 | if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2109 | return V; |
| 2110 | break; |
| 2111 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2112 | case CmpInst::ICMP_SGT: { |
| 2113 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2114 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2115 | // tested in the max/min; if so, we can just return that. |
| 2116 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2117 | return V; |
| 2118 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2119 | return V; |
| 2120 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2121 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2122 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2123 | return V; |
| 2124 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2125 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2126 | case CmpInst::ICMP_SGE: |
| 2127 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2128 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2129 | case CmpInst::ICMP_SLT: |
| 2130 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2131 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2135 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2136 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2137 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2138 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
| 2139 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2140 | // We analyze this as umax(A, B) pred A. |
| 2141 | P = Pred; |
| 2142 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2143 | (A == LHS || B == LHS)) { |
| 2144 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
| 2145 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2146 | // We analyze this as umax(A, B) swapped-pred A. |
| 2147 | P = CmpInst::getSwappedPredicate(Pred); |
| 2148 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2149 | (A == RHS || B == RHS)) { |
| 2150 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
| 2151 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2152 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2153 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2154 | P = CmpInst::getSwappedPredicate(Pred); |
| 2155 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2156 | (A == LHS || B == LHS)) { |
| 2157 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
| 2158 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2159 | // We analyze this as umax(-A, -B) pred -A. |
| 2160 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2161 | P = Pred; |
| 2162 | } |
| 2163 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2164 | // Cases correspond to "max(A, B) p A". |
| 2165 | switch (P) { |
| 2166 | default: |
| 2167 | break; |
| 2168 | case CmpInst::ICMP_EQ: |
| 2169 | case CmpInst::ICMP_ULE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2170 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2171 | // in the max/min; if so, we can just return that. |
| 2172 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2173 | return V; |
| 2174 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2175 | return V; |
| 2176 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2177 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2178 | if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2179 | return V; |
| 2180 | break; |
| 2181 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2182 | case CmpInst::ICMP_UGT: { |
| 2183 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2184 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2185 | // tested in the max/min; if so, we can just return that. |
| 2186 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2187 | return V; |
| 2188 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2189 | return V; |
| 2190 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2191 | if (MaxRecurse) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2192 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2193 | return V; |
| 2194 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2195 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2196 | case CmpInst::ICMP_UGE: |
| 2197 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2198 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2199 | case CmpInst::ICMP_ULT: |
| 2200 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2201 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2205 | // Variants on "max(x,y) >= min(x,z)". |
| 2206 | Value *C, *D; |
| 2207 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2208 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 2209 | (A == C || A == D || B == C || B == D)) { |
| 2210 | // max(x, ?) pred min(x, ?). |
| 2211 | if (Pred == CmpInst::ICMP_SGE) |
| 2212 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2213 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2214 | if (Pred == CmpInst::ICMP_SLT) |
| 2215 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2216 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2217 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2218 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 2219 | (A == C || A == D || B == C || B == D)) { |
| 2220 | // min(x, ?) pred max(x, ?). |
| 2221 | if (Pred == CmpInst::ICMP_SLE) |
| 2222 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2223 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2224 | if (Pred == CmpInst::ICMP_SGT) |
| 2225 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2226 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2227 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2228 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 2229 | (A == C || A == D || B == C || B == D)) { |
| 2230 | // max(x, ?) pred min(x, ?). |
| 2231 | if (Pred == CmpInst::ICMP_UGE) |
| 2232 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2233 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2234 | if (Pred == CmpInst::ICMP_ULT) |
| 2235 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2236 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2237 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2238 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 2239 | (A == C || A == D || B == C || B == D)) { |
| 2240 | // min(x, ?) pred max(x, ?). |
| 2241 | if (Pred == CmpInst::ICMP_ULE) |
| 2242 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2243 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2244 | if (Pred == CmpInst::ICMP_UGT) |
| 2245 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2246 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 2249 | // Simplify comparisons of GEPs. |
| 2250 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 2251 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 2252 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 2253 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 2254 | (ICmpInst::isEquality(Pred) || |
| 2255 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 2256 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 2257 | // The bases are equal and the indices are constant. Build a constant |
| 2258 | // expression GEP with the same indices and a null base pointer to see |
| 2259 | // what constant folding can make out of it. |
| 2260 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 2261 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
| 2262 | Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); |
| 2263 | |
| 2264 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
| 2265 | Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS); |
| 2266 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2271 | // If the comparison is with the result of a select instruction, check whether |
| 2272 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2273 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2274 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2275 | return V; |
| 2276 | |
| 2277 | // If the comparison is with the result of a phi instruction, check whether |
| 2278 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2279 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2280 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2281 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2283 | return 0; |
| 2284 | } |
| 2285 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2286 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2287 | const TargetData *TD, |
| 2288 | const TargetLibraryInfo *TLI, |
| 2289 | const DominatorTree *DT) { |
| 2290 | return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2293 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 2294 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2295 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2296 | const TargetData *TD, |
| 2297 | const TargetLibraryInfo *TLI, |
| 2298 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2299 | unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2300 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 2301 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 2302 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2303 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2304 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2305 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2306 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2307 | // If we have a constant, make sure it is on the RHS. |
| 2308 | std::swap(LHS, RHS); |
| 2309 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2310 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2312 | // Fold trivial predicates. |
| 2313 | if (Pred == FCmpInst::FCMP_FALSE) |
| 2314 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2315 | if (Pred == FCmpInst::FCMP_TRUE) |
| 2316 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2317 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2318 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 2319 | return UndefValue::get(GetCompareTy(LHS)); |
| 2320 | |
| 2321 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2322 | if (LHS == RHS) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2323 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 2324 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2325 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 2326 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2327 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2329 | // Handle fcmp with constant RHS |
| 2330 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2331 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 2332 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 2333 | if (CFP->getValueAPF().isNaN()) { |
| 2334 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 2335 | return ConstantInt::getFalse(CFP->getContext()); |
| 2336 | assert(FCmpInst::isUnordered(Pred) && |
| 2337 | "Comparison must be either ordered or unordered!"); |
| 2338 | // True if unordered. |
| 2339 | return ConstantInt::getTrue(CFP->getContext()); |
| 2340 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 2341 | // Check whether the constant is an infinity. |
| 2342 | if (CFP->getValueAPF().isInfinity()) { |
| 2343 | if (CFP->getValueAPF().isNegative()) { |
| 2344 | switch (Pred) { |
| 2345 | case FCmpInst::FCMP_OLT: |
| 2346 | // No value is ordered and less than negative infinity. |
| 2347 | return ConstantInt::getFalse(CFP->getContext()); |
| 2348 | case FCmpInst::FCMP_UGE: |
| 2349 | // All values are unordered with or at least negative infinity. |
| 2350 | return ConstantInt::getTrue(CFP->getContext()); |
| 2351 | default: |
| 2352 | break; |
| 2353 | } |
| 2354 | } else { |
| 2355 | switch (Pred) { |
| 2356 | case FCmpInst::FCMP_OGT: |
| 2357 | // No value is ordered and greater than infinity. |
| 2358 | return ConstantInt::getFalse(CFP->getContext()); |
| 2359 | case FCmpInst::FCMP_ULE: |
| 2360 | // All values are unordered with and at most infinity. |
| 2361 | return ConstantInt::getTrue(CFP->getContext()); |
| 2362 | default: |
| 2363 | break; |
| 2364 | } |
| 2365 | } |
| 2366 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2369 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2370 | // If the comparison is with the result of a select instruction, check whether |
| 2371 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2372 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2373 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2374 | return V; |
| 2375 | |
| 2376 | // If the comparison is with the result of a phi instruction, check whether |
| 2377 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2378 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2379 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2380 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2382 | return 0; |
| 2383 | } |
| 2384 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2385 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2386 | const TargetData *TD, |
| 2387 | const TargetLibraryInfo *TLI, |
| 2388 | const DominatorTree *DT) { |
| 2389 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2392 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 2393 | /// the result. If not, this returns null. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2394 | Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, |
| 2395 | const TargetData *TD, const DominatorTree *) { |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2396 | // select true, X, Y -> X |
| 2397 | // select false, X, Y -> Y |
| 2398 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 2399 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2401 | // select C, X, X -> X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2402 | if (TrueVal == FalseVal) |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2403 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2404 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2405 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 2406 | if (isa<Constant>(TrueVal)) |
| 2407 | return TrueVal; |
| 2408 | return FalseVal; |
| 2409 | } |
Dan Gohman | 68c0dbc | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 2410 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 2411 | return FalseVal; |
| 2412 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 2413 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2415 | return 0; |
| 2416 | } |
| 2417 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2418 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 2419 | /// fold the result. If not, this returns null. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2420 | Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const TargetData *TD, |
| 2421 | const DominatorTree *) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2422 | // The type of the GEP pointer operand. |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2423 | PointerType *PtrTy = dyn_cast<PointerType>(Ops[0]->getType()); |
| 2424 | // The GEP pointer operand is not a pointer, it's a vector of pointers. |
| 2425 | if (!PtrTy) |
| 2426 | return 0; |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2428 | // getelementptr P -> P. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2429 | if (Ops.size() == 1) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2430 | return Ops[0]; |
| 2431 | |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2432 | if (isa<UndefValue>(Ops[0])) { |
| 2433 | // Compute the (pointer) type returned by the GEP instruction. |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2434 | Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2435 | Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2436 | return UndefValue::get(GEPTy); |
| 2437 | } |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2438 | |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2439 | if (Ops.size() == 2) { |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2440 | // getelementptr P, 0 -> P. |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2441 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 2442 | if (C->isZero()) |
| 2443 | return Ops[0]; |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2444 | // getelementptr P, N -> P if P points to a type of zero size. |
| 2445 | if (TD) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2446 | Type *Ty = PtrTy->getElementType(); |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 2447 | if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0) |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2448 | return Ops[0]; |
| 2449 | } |
| 2450 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2451 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2452 | // Check to see if this is constant foldable. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2453 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2454 | if (!isa<Constant>(Ops[i])) |
| 2455 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2456 | |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2457 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1)); |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2460 | /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we |
| 2461 | /// can fold the result. If not, this returns null. |
| 2462 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2463 | ArrayRef<unsigned> Idxs, |
| 2464 | const TargetData *, |
| 2465 | const DominatorTree *) { |
| 2466 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 2467 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 2468 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 2469 | |
| 2470 | // insertvalue x, undef, n -> x |
| 2471 | if (match(Val, m_Undef())) |
| 2472 | return Agg; |
| 2473 | |
| 2474 | // insertvalue x, (extractvalue y, n), n |
| 2475 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | ae707bd | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 2476 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 2477 | EV->getIndices() == Idxs) { |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2478 | // insertvalue undef, (extractvalue y, n), n -> y |
| 2479 | if (match(Agg, m_Undef())) |
| 2480 | return EV->getAggregateOperand(); |
| 2481 | |
| 2482 | // insertvalue y, (extractvalue y, n), n -> y |
| 2483 | if (Agg == EV->getAggregateOperand()) |
| 2484 | return Agg; |
| 2485 | } |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2490 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
| 2491 | static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) { |
| 2492 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 2493 | // with the common value. |
| 2494 | Value *CommonValue = 0; |
| 2495 | bool HasUndefInput = false; |
| 2496 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2497 | Value *Incoming = PN->getIncomingValue(i); |
| 2498 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 2499 | if (Incoming == PN) continue; |
| 2500 | if (isa<UndefValue>(Incoming)) { |
| 2501 | // Remember that we saw an undef value, but otherwise ignore them. |
| 2502 | HasUndefInput = true; |
| 2503 | continue; |
| 2504 | } |
| 2505 | if (CommonValue && Incoming != CommonValue) |
| 2506 | return 0; // Not the same, bail out. |
| 2507 | CommonValue = Incoming; |
| 2508 | } |
| 2509 | |
| 2510 | // If CommonValue is null then all of the incoming values were either undef or |
| 2511 | // equal to the phi node itself. |
| 2512 | if (!CommonValue) |
| 2513 | return UndefValue::get(PN->getType()); |
| 2514 | |
| 2515 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 2516 | // instruction, we cannot return X as the result of the PHI node unless it |
| 2517 | // dominates the PHI block. |
| 2518 | if (HasUndefInput) |
| 2519 | return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0; |
| 2520 | |
| 2521 | return CommonValue; |
| 2522 | } |
| 2523 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2524 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2525 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2526 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 2527 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2528 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2529 | const TargetData *TD, |
| 2530 | const TargetLibraryInfo *TLI, |
| 2531 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2532 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2533 | switch (Opcode) { |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2534 | case Instruction::Add: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2535 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2536 | TD, TLI, DT, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2537 | case Instruction::Sub: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2538 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2539 | TD, TLI, DT, MaxRecurse); |
| 2540 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, TD, TLI, DT, |
| 2541 | MaxRecurse); |
| 2542 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, TLI, DT, |
| 2543 | MaxRecurse); |
| 2544 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, TLI, DT, |
| 2545 | MaxRecurse); |
| 2546 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, TD, TLI, DT, |
| 2547 | MaxRecurse); |
| 2548 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, TD, TLI, DT, |
| 2549 | MaxRecurse); |
| 2550 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, TD, TLI, DT, |
| 2551 | MaxRecurse); |
| 2552 | case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, TD, TLI, DT, |
| 2553 | MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2554 | case Instruction::Shl: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2555 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2556 | TD, TLI, DT, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2557 | case Instruction::LShr: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2558 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT, |
| 2559 | MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2560 | case Instruction::AShr: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2561 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT, |
| 2562 | MaxRecurse); |
| 2563 | case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, TLI, DT, |
| 2564 | MaxRecurse); |
| 2565 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, TD, TLI, DT, |
| 2566 | MaxRecurse); |
| 2567 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, TLI, DT, |
| 2568 | MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2569 | default: |
| 2570 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 2571 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 2572 | Constant *COps[] = {CLHS, CRHS}; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2573 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, TD, TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2574 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2575 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2576 | // If the operation is associative, try some generic simplifications. |
| 2577 | if (Instruction::isAssociative(Opcode)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2578 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, TLI, DT, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2579 | MaxRecurse)) |
| 2580 | return V; |
| 2581 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2582 | // If the operation is with the result of a select instruction, check whether |
| 2583 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2584 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2585 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, TLI, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2586 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2587 | return V; |
| 2588 | |
| 2589 | // If the operation is with the result of a phi instruction, check whether |
| 2590 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2591 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2592 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, TLI, DT, |
| 2593 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2594 | return V; |
| 2595 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2596 | return 0; |
| 2597 | } |
| 2598 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2599 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2600 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2601 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 2602 | const DominatorTree *DT) { |
| 2603 | return ::SimplifyBinOp(Opcode, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2606 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 2607 | /// fold the result. |
| 2608 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2609 | const TargetData *TD, |
| 2610 | const TargetLibraryInfo *TLI, |
| 2611 | const DominatorTree *DT, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 2612 | unsigned MaxRecurse) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2613 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2614 | return SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse); |
| 2615 | return SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2619 | const TargetData *TD, const TargetLibraryInfo *TLI, |
| 2620 | const DominatorTree *DT) { |
| 2621 | return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2622 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2623 | |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2624 | static Value *SimplifyCallInst(CallInst *CI) { |
| 2625 | // call undef -> undef |
| 2626 | if (isa<UndefValue>(CI->getCalledValue())) |
| 2627 | return UndefValue::get(CI->getType()); |
| 2628 | |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2632 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 2633 | /// instruction. If not, this returns null. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2634 | Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2635 | const TargetLibraryInfo *TLI, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2636 | const DominatorTree *DT) { |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2637 | Value *Result; |
| 2638 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2639 | switch (I->getOpcode()) { |
| 2640 | default: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2641 | Result = ConstantFoldInstruction(I, TD, TLI); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2642 | break; |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 2643 | case Instruction::Add: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2644 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 2645 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2646 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2647 | TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2648 | break; |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 2649 | case Instruction::Sub: |
| 2650 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 2651 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2652 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2653 | TD, TLI, DT); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 2654 | break; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 2655 | case Instruction::Mul: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2656 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 2657 | break; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2658 | case Instruction::SDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2659 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2660 | break; |
| 2661 | case Instruction::UDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2662 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 2663 | break; |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 2664 | case Instruction::FDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2665 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 2666 | break; |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2667 | case Instruction::SRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2668 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2669 | break; |
| 2670 | case Instruction::URem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2671 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | case Instruction::FRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2674 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 2675 | break; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2676 | case Instruction::Shl: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2677 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 2678 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 2679 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2680 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | case Instruction::LShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2683 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
| 2684 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2685 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | case Instruction::AShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2688 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
| 2689 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2690 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 2691 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2692 | case Instruction::And: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2693 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2694 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2695 | case Instruction::Or: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2696 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2697 | break; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2698 | case Instruction::Xor: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2699 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2700 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2701 | case Instruction::ICmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2702 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2703 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2704 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2705 | case Instruction::FCmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2706 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2707 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2708 | break; |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2709 | case Instruction::Select: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2710 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
| 2711 | I->getOperand(2), TD, DT); |
| 2712 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2713 | case Instruction::GetElementPtr: { |
| 2714 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2715 | Result = SimplifyGEPInst(Ops, TD, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2716 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2717 | } |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2718 | case Instruction::InsertValue: { |
| 2719 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 2720 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 2721 | IV->getInsertedValueOperand(), |
| 2722 | IV->getIndices(), TD, DT); |
| 2723 | break; |
| 2724 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 2725 | case Instruction::PHI: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2726 | Result = SimplifyPHINode(cast<PHINode>(I), DT); |
| 2727 | break; |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2728 | case Instruction::Call: |
| 2729 | Result = SimplifyCallInst(cast<CallInst>(I)); |
| 2730 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2731 | } |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 2732 | |
| 2733 | /// If called on unreachable code, the above logic may report that the |
| 2734 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 2735 | /// detecting that case here, returning a safe value instead. |
| 2736 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2739 | /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then |
| 2740 | /// delete the From instruction. In addition to a basic RAUW, this does a |
| 2741 | /// recursive simplification of the newly formed instructions. This catches |
| 2742 | /// things where one simplification exposes other opportunities. This only |
| 2743 | /// simplifies and deletes scalar operations, it does not change the CFG. |
| 2744 | /// |
| 2745 | void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2746 | const TargetData *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2747 | const TargetLibraryInfo *TLI, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 2748 | const DominatorTree *DT) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2749 | assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2751 | // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that |
| 2752 | // we can know if it gets deleted out from under us or replaced in a |
| 2753 | // recursive simplification. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2754 | WeakVH FromHandle(From); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2755 | WeakVH ToHandle(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2756 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2757 | while (!From->use_empty()) { |
| 2758 | // Update the instruction to use the new value. |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2759 | Use &TheUse = From->use_begin().getUse(); |
| 2760 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
| 2761 | TheUse = To; |
| 2762 | |
| 2763 | // Check to see if the instruction can be folded due to the operand |
| 2764 | // replacement. For example changing (or X, Y) into (or X, -1) can replace |
| 2765 | // the 'or' with -1. |
| 2766 | Value *SimplifiedVal; |
| 2767 | { |
| 2768 | // Sanity check to make sure 'User' doesn't dangle across |
| 2769 | // SimplifyInstruction. |
| 2770 | AssertingVH<> UserHandle(User); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2771 | |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2772 | SimplifiedVal = SimplifyInstruction(User, TD, TLI, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2773 | if (SimplifiedVal == 0) continue; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2774 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2775 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2776 | // Recursively simplify this user to the new value. |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2777 | ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, TLI, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2778 | From = dyn_cast_or_null<Instruction>((Value*)FromHandle); |
| 2779 | To = ToHandle; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2780 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2781 | assert(ToHandle && "To value deleted by recursive simplification?"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2783 | // If the recursive simplification ended up revisiting and deleting |
| 2784 | // 'From' then we're done. |
| 2785 | if (From == 0) |
| 2786 | return; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2787 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2788 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 2789 | // If 'From' has value handles referring to it, do a real RAUW to update them. |
| 2790 | From->replaceAllUsesWith(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 2792 | From->eraseFromParent(); |
| 2793 | } |