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