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