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