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" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SetVector.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/GlobalAlias.h" |
| 30 | #include "llvm/IR/Operator.h" |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ConstantRange.h" |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 32 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/PatternMatch.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ValueHandle.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 { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 45 | const DataLayout *TD; |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 46 | const TargetLibraryInfo *TLI; |
| 47 | const DominatorTree *DT; |
| 48 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 49 | Query(const DataLayout *td, const TargetLibraryInfo *tli, |
Bill Wendling | 9133783 | 2012-05-17 20:27:58 +0000 | [diff] [blame] | 50 | const DominatorTree *dt) : TD(td), TLI(tli), DT(dt) {} |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 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, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 654 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 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 | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 660 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 661 | /// |
| 662 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 663 | /// accumulates the total constant offset applied in the returned constant. It |
| 664 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 665 | /// no constant offsets applied. |
Dan Gohman | 819f9d6 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 666 | /// |
| 667 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 668 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 669 | /// folding. |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 670 | static Constant *stripAndComputeConstantOffsets(const DataLayout *TD, |
| 671 | Value *&V) { |
| 672 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 673 | |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 674 | // Without DataLayout, just be conservative for now. Theoretically, more could |
| 675 | // be done in this case. |
| 676 | if (!TD) |
| 677 | return ConstantInt::get(IntegerType::get(V->getContext(), 64), 0); |
| 678 | |
Matt Arsenault | 7eef3bd | 2013-08-02 00:10:44 +0000 | [diff] [blame] | 679 | unsigned AS = V->getType()->getPointerAddressSpace(); |
| 680 | unsigned IntPtrWidth = TD->getPointerSizeInBits(AS); |
Chandler Carruth | 90c14fc | 2012-03-13 00:06:15 +0000 | [diff] [blame] | 681 | APInt Offset = APInt::getNullValue(IntPtrWidth); |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 682 | |
| 683 | // Even though we don't look through PHI nodes, we could be called on an |
| 684 | // instruction in an unreachable block, which may be on a cycle. |
| 685 | SmallPtrSet<Value *, 4> Visited; |
| 686 | Visited.insert(V); |
| 687 | do { |
| 688 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 689 | if (!GEP->isInBounds() || !GEP->accumulateConstantOffset(*TD, Offset)) |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 690 | break; |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 691 | V = GEP->getPointerOperand(); |
| 692 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 7eef3bd | 2013-08-02 00:10:44 +0000 | [diff] [blame] | 693 | Value *Op0 = cast<Operator>(V)->getOperand(0); |
| 694 | assert(TD->getPointerTypeSizeInBits(V->getType()) == |
| 695 | TD->getPointerTypeSizeInBits(Op0->getType()) && |
| 696 | "Bitcasting between pointers from different size address spaces"); |
| 697 | V = Op0; |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 698 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 699 | if (GA->mayBeOverridden()) |
| 700 | break; |
| 701 | V = GA->getAliasee(); |
| 702 | } else { |
| 703 | break; |
| 704 | } |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 705 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 706 | "Unexpected operand type!"); |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 707 | } while (Visited.insert(V)); |
| 708 | |
Matt Arsenault | 7eef3bd | 2013-08-02 00:10:44 +0000 | [diff] [blame] | 709 | Type *IntPtrTy = TD->getIntPtrType(V->getContext(), AS); |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 710 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 711 | if (V->getType()->isVectorTy()) |
| 712 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 713 | OffsetIntPtr); |
| 714 | return OffsetIntPtr; |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /// \brief Compute the constant difference between two pointer values. |
| 718 | /// If the difference is not a constant, returns zero. |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 719 | static Constant *computePointerDifference(const DataLayout *TD, |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 720 | Value *LHS, Value *RHS) { |
| 721 | Constant *LHSOffset = stripAndComputeConstantOffsets(TD, LHS); |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 722 | Constant *RHSOffset = stripAndComputeConstantOffsets(TD, RHS); |
Chandler Carruth | fc72ae6 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 723 | |
| 724 | // If LHS and RHS are not related via constant offsets to the same base |
| 725 | // value, there is nothing we can do here. |
| 726 | if (LHS != RHS) |
| 727 | return 0; |
| 728 | |
| 729 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 730 | // LHS - RHS |
| 731 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 732 | // = LHSOffset - RHSOffset |
| 733 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 734 | } |
| 735 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 736 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 737 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 738 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 739 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 740 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 741 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 742 | Constant *Ops[] = { CLHS, CRHS }; |
| 743 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 744 | Ops, Q.TD, Q.TLI); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // X - undef -> undef |
| 748 | // undef - X -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 749 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 750 | return UndefValue::get(Op0->getType()); |
| 751 | |
| 752 | // X - 0 -> X |
| 753 | if (match(Op1, m_Zero())) |
| 754 | return Op0; |
| 755 | |
| 756 | // X - X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 757 | if (Op0 == Op1) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 758 | return Constant::getNullValue(Op0->getType()); |
| 759 | |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 760 | // (X*2) - X -> X |
| 761 | // (X<<1) - X -> X |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 762 | Value *X = 0; |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 763 | if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) || |
| 764 | match(Op0, m_Shl(m_Specific(Op1), m_One()))) |
| 765 | return Op1; |
| 766 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 767 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 768 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
| 769 | Value *Y = 0, *Z = Op1; |
| 770 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 771 | // See if "V === Y - Z" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 772 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 773 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 774 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 775 | // It does, we successfully reassociated! |
| 776 | ++NumReassoc; |
| 777 | return W; |
| 778 | } |
| 779 | // See if "V === X - Z" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 780 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 781 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 782 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 783 | // It does, we successfully reassociated! |
| 784 | ++NumReassoc; |
| 785 | return W; |
| 786 | } |
| 787 | } |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 788 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 789 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 790 | // For example, X - (X + 1) -> -1 |
| 791 | X = Op0; |
| 792 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 793 | // See if "V === X - Y" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 794 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 795 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 796 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 797 | // It does, we successfully reassociated! |
| 798 | ++NumReassoc; |
| 799 | return W; |
| 800 | } |
| 801 | // See if "V === X - Z" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 802 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 803 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 804 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 805 | // It does, we successfully reassociated! |
| 806 | ++NumReassoc; |
| 807 | return W; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 812 | // For example, X - (X - Y) -> Y. |
| 813 | Z = Op0; |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 814 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 815 | // See if "V === Z - X" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 816 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 817 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 818 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 819 | // It does, we successfully reassociated! |
| 820 | ++NumReassoc; |
| 821 | return W; |
| 822 | } |
| 823 | |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 824 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 825 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 826 | match(Op1, m_Trunc(m_Value(Y)))) |
| 827 | if (X->getType() == Y->getType()) |
| 828 | // See if "V === X - Y" simplifies. |
| 829 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 830 | // It does! Now see if "trunc V" simplifies. |
| 831 | if (Value *W = SimplifyTruncInst(V, Op0->getType(), Q, MaxRecurse-1)) |
| 832 | // It does, return the simplified "trunc V". |
| 833 | return W; |
| 834 | |
| 835 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 836 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 837 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 838 | if (Constant *Result = computePointerDifference(Q.TD, X, Y)) |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 839 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 840 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 841 | // Mul distributes over Sub. Try some generic simplifications based on this. |
| 842 | if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 843 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 844 | return V; |
| 845 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 846 | // i1 sub -> xor. |
| 847 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 848 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 849 | return V; |
| 850 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 851 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 852 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 853 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 854 | // only if B and C are equal. If B and C are equal then (since we assume |
| 855 | // that operands have already been simplified) "select(cond, B, C)" should |
| 856 | // have been simplified to the common value of B and C already. Analysing |
| 857 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 858 | // for threading over phi nodes. |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 863 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 864 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 865 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 866 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query (TD, TLI, DT), |
| 867 | RecursionLimit); |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 870 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 871 | /// returns null. |
| 872 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 873 | const Query &Q, unsigned MaxRecurse) { |
| 874 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 875 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 876 | Constant *Ops[] = { CLHS, CRHS }; |
| 877 | return ConstantFoldInstOperands(Instruction::FAdd, CLHS->getType(), |
| 878 | Ops, Q.TD, Q.TLI); |
| 879 | } |
| 880 | |
| 881 | // Canonicalize the constant to the RHS. |
| 882 | std::swap(Op0, Op1); |
| 883 | } |
| 884 | |
| 885 | // fadd X, -0 ==> X |
| 886 | if (match(Op1, m_NegZero())) |
| 887 | return Op0; |
| 888 | |
| 889 | // fadd X, 0 ==> X, when we know X is not -0 |
| 890 | if (match(Op1, m_Zero()) && |
| 891 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 892 | return Op0; |
| 893 | |
| 894 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 895 | // where nnan and ninf have to occur at least once somewhere in this |
| 896 | // expression |
| 897 | Value *SubOp = 0; |
| 898 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 899 | SubOp = Op1; |
| 900 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 901 | SubOp = Op0; |
| 902 | if (SubOp) { |
| 903 | Instruction *FSub = cast<Instruction>(SubOp); |
| 904 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 905 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 906 | return Constant::getNullValue(Op0->getType()); |
| 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 913 | /// returns null. |
| 914 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 915 | const Query &Q, unsigned MaxRecurse) { |
| 916 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 917 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 918 | Constant *Ops[] = { CLHS, CRHS }; |
| 919 | return ConstantFoldInstOperands(Instruction::FSub, CLHS->getType(), |
| 920 | Ops, Q.TD, Q.TLI); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | // fsub X, 0 ==> X |
| 925 | if (match(Op1, m_Zero())) |
| 926 | return Op0; |
| 927 | |
| 928 | // fsub X, -0 ==> X, when we know X is not -0 |
| 929 | if (match(Op1, m_NegZero()) && |
| 930 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 931 | return Op0; |
| 932 | |
| 933 | // fsub 0, (fsub -0.0, X) ==> X |
| 934 | Value *X; |
| 935 | if (match(Op0, m_AnyZero())) { |
| 936 | if (match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 937 | return X; |
| 938 | if (FMF.noSignedZeros() && match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 939 | return X; |
| 940 | } |
| 941 | |
| 942 | // fsub nnan ninf x, x ==> 0.0 |
| 943 | if (FMF.noNaNs() && FMF.noInfs() && Op0 == Op1) |
| 944 | return Constant::getNullValue(Op0->getType()); |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
Michael Ilseman | eb61c92 | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 949 | /// Given the operands for an FMul, see if we can fold the result |
| 950 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, |
| 951 | FastMathFlags FMF, |
| 952 | const Query &Q, |
| 953 | unsigned MaxRecurse) { |
| 954 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 955 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 956 | Constant *Ops[] = { CLHS, CRHS }; |
| 957 | return ConstantFoldInstOperands(Instruction::FMul, CLHS->getType(), |
| 958 | Ops, Q.TD, Q.TLI); |
| 959 | } |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 960 | |
| 961 | // Canonicalize the constant to the RHS. |
| 962 | std::swap(Op0, Op1); |
Michael Ilseman | eb61c92 | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 965 | // fmul X, 1.0 ==> X |
| 966 | if (match(Op1, m_FPOne())) |
| 967 | return Op0; |
| 968 | |
| 969 | // fmul nnan nsz X, 0 ==> 0 |
| 970 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 971 | return Op1; |
Michael Ilseman | eb61c92 | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 976 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 977 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 978 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 979 | unsigned MaxRecurse) { |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 980 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 981 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 982 | Constant *Ops[] = { CLHS, CRHS }; |
| 983 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 984 | Ops, Q.TD, Q.TLI); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | // Canonicalize the constant to the RHS. |
| 988 | std::swap(Op0, Op1); |
| 989 | } |
| 990 | |
| 991 | // X * undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 992 | if (match(Op1, m_Undef())) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 993 | return Constant::getNullValue(Op0->getType()); |
| 994 | |
| 995 | // X * 0 -> 0 |
| 996 | if (match(Op1, m_Zero())) |
| 997 | return Op1; |
| 998 | |
| 999 | // X * 1 -> X |
| 1000 | if (match(Op1, m_One())) |
| 1001 | return Op0; |
| 1002 | |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 1003 | // (X / Y) * Y -> X if the division is exact. |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1004 | Value *X = 0; |
| 1005 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 1006 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 1007 | return X; |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 1008 | |
Nick Lewycky | 5413880 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 1009 | // i1 mul -> and. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 1010 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1011 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 1012 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Try some generic simplifications for associative operations. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1015 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1016 | MaxRecurse)) |
| 1017 | return V; |
| 1018 | |
| 1019 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 1020 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1021 | Q, MaxRecurse)) |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1022 | return V; |
| 1023 | |
| 1024 | // If the operation is with the result of a select instruction, check whether |
| 1025 | // operating on either branch of the select always yields the same value. |
| 1026 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1027 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1028 | MaxRecurse)) |
| 1029 | return V; |
| 1030 | |
| 1031 | // If the operation is with the result of a phi instruction, check whether |
| 1032 | // operating on all incoming values of the phi always yields the same value. |
| 1033 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1034 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1035 | MaxRecurse)) |
| 1036 | return V; |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 1041 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1042 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
| 1043 | const DominatorTree *DT) { |
| 1044 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit); |
| 1045 | } |
| 1046 | |
| 1047 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1048 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
| 1049 | const DominatorTree *DT) { |
| 1050 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit); |
| 1051 | } |
| 1052 | |
Michael Ilseman | eb61c92 | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 1053 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, |
| 1054 | FastMathFlags FMF, |
| 1055 | const DataLayout *TD, |
| 1056 | const TargetLibraryInfo *TLI, |
| 1057 | const DominatorTree *DT) { |
| 1058 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query (TD, TLI, DT), RecursionLimit); |
| 1059 | } |
| 1060 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1061 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1062 | const TargetLibraryInfo *TLI, |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1063 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1064 | return ::SimplifyMulInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1067 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 1068 | /// fold the result. If not, this returns null. |
Anders Carlsson | 479b4b9 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 1069 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1070 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1071 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1072 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1073 | Constant *Ops[] = { C0, C1 }; |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1074 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1078 | bool isSigned = Opcode == Instruction::SDiv; |
| 1079 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1080 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1081 | if (match(Op1, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1082 | return Op1; |
| 1083 | |
| 1084 | // undef / X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1085 | if (match(Op0, m_Undef())) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1086 | return Constant::getNullValue(Op0->getType()); |
| 1087 | |
| 1088 | // 0 / X -> 0, we don't need to preserve faults! |
| 1089 | if (match(Op0, m_Zero())) |
| 1090 | return Op0; |
| 1091 | |
| 1092 | // X / 1 -> X |
| 1093 | if (match(Op1, m_One())) |
| 1094 | return Op0; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (Op0->getType()->isIntegerTy(1)) |
| 1097 | // It can't be division by zero, hence it must be division by one. |
| 1098 | return Op0; |
| 1099 | |
| 1100 | // X / X -> 1 |
| 1101 | if (Op0 == Op1) |
| 1102 | return ConstantInt::get(Op0->getType(), 1); |
| 1103 | |
| 1104 | // (X * Y) / Y -> X if the multiplication does not overflow. |
| 1105 | Value *X = 0, *Y = 0; |
| 1106 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1107 | 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] | 1108 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 4b72071 | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1109 | // If the Mul knows it does not overflow, then we are good to go. |
| 1110 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1111 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1112 | return X; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1113 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1114 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1115 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1116 | return X; |
| 1117 | } |
| 1118 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1119 | // (X rem Y) / Y -> 0 |
| 1120 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1121 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1122 | return Constant::getNullValue(Op0->getType()); |
| 1123 | |
| 1124 | // If the operation is with the result of a select instruction, check whether |
| 1125 | // operating on either branch of the select always yields the same value. |
| 1126 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1127 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1128 | return V; |
| 1129 | |
| 1130 | // If the operation is with the result of a phi instruction, check whether |
| 1131 | // operating on all incoming values of the phi always yields the same value. |
| 1132 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1133 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1134 | return V; |
| 1135 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 1140 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1141 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1142 | unsigned MaxRecurse) { |
| 1143 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1144 | return V; |
| 1145 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1149 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1150 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1151 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1152 | return ::SimplifySDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 1156 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1157 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1158 | unsigned MaxRecurse) { |
| 1159 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1160 | return V; |
| 1161 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1165 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1166 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1167 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1168 | return ::SimplifyUDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1171 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1172 | unsigned) { |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1173 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1174 | if (match(Op0, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1175 | return Op0; |
| 1176 | |
| 1177 | // X / undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1178 | if (match(Op1, m_Undef())) |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1179 | return Op1; |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1184 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1185 | const TargetLibraryInfo *TLI, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1186 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1187 | return ::SimplifyFDivInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1190 | /// SimplifyRem - Given operands for an SRem or URem, see if we can |
| 1191 | /// fold the result. If not, this returns null. |
| 1192 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1193 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1194 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1195 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1196 | Constant *Ops[] = { C0, C1 }; |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1197 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1201 | // X % undef -> undef |
| 1202 | if (match(Op1, m_Undef())) |
| 1203 | return Op1; |
| 1204 | |
| 1205 | // undef % X -> 0 |
| 1206 | if (match(Op0, m_Undef())) |
| 1207 | return Constant::getNullValue(Op0->getType()); |
| 1208 | |
| 1209 | // 0 % X -> 0, we don't need to preserve faults! |
| 1210 | if (match(Op0, m_Zero())) |
| 1211 | return Op0; |
| 1212 | |
| 1213 | // X % 0 -> undef, we don't need to preserve faults! |
| 1214 | if (match(Op1, m_Zero())) |
| 1215 | return UndefValue::get(Op0->getType()); |
| 1216 | |
| 1217 | // X % 1 -> 0 |
| 1218 | if (match(Op1, m_One())) |
| 1219 | return Constant::getNullValue(Op0->getType()); |
| 1220 | |
| 1221 | if (Op0->getType()->isIntegerTy(1)) |
| 1222 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1223 | return Constant::getNullValue(Op0->getType()); |
| 1224 | |
| 1225 | // X % X -> 0 |
| 1226 | if (Op0 == Op1) |
| 1227 | return Constant::getNullValue(Op0->getType()); |
| 1228 | |
| 1229 | // If the operation is with the result of a select instruction, check whether |
| 1230 | // operating on either branch of the select always yields the same value. |
| 1231 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1232 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1233 | return V; |
| 1234 | |
| 1235 | // If the operation is with the result of a phi instruction, check whether |
| 1236 | // operating on all incoming values of the phi always yields the same value. |
| 1237 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1238 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1239 | return V; |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | /// SimplifySRemInst - Given operands for an SRem, see if we can |
| 1245 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1246 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1247 | unsigned MaxRecurse) { |
| 1248 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1249 | return V; |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1254 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1255 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1256 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1257 | return ::SimplifySRemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /// SimplifyURemInst - Given operands for a URem, see if we can |
| 1261 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1262 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1263 | unsigned MaxRecurse) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1264 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1265 | return V; |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1270 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1271 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1272 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1273 | return ::SimplifyURemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1276 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const Query &, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1277 | unsigned) { |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1278 | // undef % X -> undef (the undef could be a snan). |
| 1279 | if (match(Op0, m_Undef())) |
| 1280 | return Op0; |
| 1281 | |
| 1282 | // X % undef -> undef |
| 1283 | if (match(Op1, m_Undef())) |
| 1284 | return Op1; |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1289 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1290 | const TargetLibraryInfo *TLI, |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1291 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1292 | return ::SimplifyFRemInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1295 | /// 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] | 1296 | /// fold the result. If not, this returns null. |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1297 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1298 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1299 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1300 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1301 | Constant *Ops[] = { C0, C1 }; |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1302 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.TD, Q.TLI); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1306 | // 0 shift by X -> 0 |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1307 | if (match(Op0, m_Zero())) |
| 1308 | return Op0; |
| 1309 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1310 | // X shift by 0 -> X |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1311 | if (match(Op1, m_Zero())) |
| 1312 | return Op0; |
| 1313 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1314 | // X shift by undef -> undef because it may shift by the bitwidth. |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1315 | if (match(Op1, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1316 | return Op1; |
| 1317 | |
| 1318 | // Shifting by the bitwidth or more is undefined. |
| 1319 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) |
| 1320 | if (CI->getValue().getLimitedValue() >= |
| 1321 | Op0->getType()->getScalarSizeInBits()) |
| 1322 | return UndefValue::get(Op0->getType()); |
| 1323 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1324 | // If the operation is with the result of a select instruction, check whether |
| 1325 | // operating on either branch of the select always yields the same value. |
| 1326 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1327 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1328 | return V; |
| 1329 | |
| 1330 | // If the operation is with the result of a phi instruction, check whether |
| 1331 | // operating on all incoming values of the phi always yields the same value. |
| 1332 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1333 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1334 | return V; |
| 1335 | |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
| 1339 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 1340 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1341 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1342 | const Query &Q, unsigned MaxRecurse) { |
| 1343 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1344 | return V; |
| 1345 | |
| 1346 | // undef << X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1347 | if (match(Op0, m_Undef())) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1348 | return Constant::getNullValue(Op0->getType()); |
| 1349 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1350 | // (X >> A) << A -> X |
| 1351 | Value *X; |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1352 | 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] | 1353 | return X; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1354 | return 0; |
| 1355 | } |
| 1356 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1357 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1358 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1359 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1360 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query (TD, TLI, DT), |
| 1361 | RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 1365 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1366 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1367 | const Query &Q, unsigned MaxRecurse) { |
| 1368 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1369 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1370 | |
David Majnemer | 8c5c6f0 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1371 | // X >> X -> 0 |
| 1372 | if (Op0 == Op1) |
| 1373 | return Constant::getNullValue(Op0->getType()); |
| 1374 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1375 | // undef >>l X -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1376 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1377 | return Constant::getNullValue(Op0->getType()); |
| 1378 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1379 | // (X << A) >> A -> X |
| 1380 | Value *X; |
| 1381 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1382 | cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap()) |
| 1383 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1384 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1388 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1389 | const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1390 | const TargetLibraryInfo *TLI, |
| 1391 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1392 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query (TD, TLI, DT), |
| 1393 | RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 1397 | /// fold the result. If not, this returns null. |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1398 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1399 | const Query &Q, unsigned MaxRecurse) { |
| 1400 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1401 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1402 | |
David Majnemer | 8c5c6f0 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1403 | // X >> X -> 0 |
| 1404 | if (Op0 == Op1) |
| 1405 | return Constant::getNullValue(Op0->getType()); |
| 1406 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1407 | // all ones >>a X -> all ones |
| 1408 | if (match(Op0, m_AllOnes())) |
| 1409 | return Op0; |
| 1410 | |
| 1411 | // undef >>a X -> all ones |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1412 | if (match(Op0, m_Undef())) |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1413 | return Constant::getAllOnesValue(Op0->getType()); |
| 1414 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1415 | // (X << A) >> A -> X |
| 1416 | Value *X; |
| 1417 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1418 | cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap()) |
| 1419 | return X; |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1420 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1424 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1425 | const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1426 | const TargetLibraryInfo *TLI, |
| 1427 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1428 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query (TD, TLI, DT), |
| 1429 | RecursionLimit); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1432 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1433 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1434 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1435 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1436 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1437 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1438 | Constant *Ops[] = { CLHS, CRHS }; |
| 1439 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1440 | Ops, Q.TD, Q.TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1441 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1442 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1443 | // Canonicalize the constant to the RHS. |
| 1444 | std::swap(Op0, Op1); |
| 1445 | } |
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 | // X & undef -> 0 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1448 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1449 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1451 | // X & X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1452 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1453 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1454 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1455 | // X & 0 = 0 |
| 1456 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1457 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1458 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1459 | // X & -1 = X |
| 1460 | if (match(Op1, m_AllOnes())) |
| 1461 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1463 | // A & ~A = ~A & A = 0 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1464 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1465 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1466 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1468 | // (A | ?) & A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1469 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1470 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1471 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1472 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1474 | // A & (A | ?) = A |
| 1475 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1476 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1477 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1478 | |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1479 | // A & (-A) = A if A is a power of two or zero. |
| 1480 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1481 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Rafael Espindola | dbaa237 | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 1482 | if (isKnownToBeAPowerOfTwo(Op0, /*OrZero*/true)) |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1483 | return Op0; |
Rafael Espindola | dbaa237 | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 1484 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true)) |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1485 | return Op1; |
| 1486 | } |
| 1487 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1488 | // Try some generic simplifications for associative operations. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1489 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1490 | MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1491 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1492 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1493 | // And distributes over Or. Try some generic simplifications based on this. |
| 1494 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1495 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1496 | return V; |
| 1497 | |
| 1498 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1499 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1500 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1501 | return V; |
| 1502 | |
| 1503 | // Or distributes over And. Try some generic simplifications based on this. |
| 1504 | if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1505 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1506 | return V; |
| 1507 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1508 | // If the operation is with the result of a select instruction, check whether |
| 1509 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1510 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1511 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1512 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1513 | return V; |
| 1514 | |
| 1515 | // If the operation is with the result of a phi instruction, check whether |
| 1516 | // 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] | 1517 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1518 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1519 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1520 | return V; |
| 1521 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1525 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1526 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1527 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1528 | return ::SimplifyAndInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1531 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1532 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1533 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1534 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1535 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1536 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1537 | Constant *Ops[] = { CLHS, CRHS }; |
| 1538 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1539 | Ops, Q.TD, Q.TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1540 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1542 | // Canonicalize the constant to the RHS. |
| 1543 | std::swap(Op0, Op1); |
| 1544 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1546 | // X | undef -> -1 |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1547 | if (match(Op1, m_Undef())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1548 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1550 | // X | X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1551 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1552 | return Op0; |
| 1553 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1554 | // X | 0 = X |
| 1555 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1556 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1557 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1558 | // X | -1 = -1 |
| 1559 | if (match(Op1, m_AllOnes())) |
| 1560 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1562 | // A | ~A = ~A | A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1563 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1564 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1565 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1567 | // (A & ?) | A = A |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1568 | Value *A = 0, *B = 0; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1569 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1570 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1571 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1573 | // A | (A & ?) = A |
| 1574 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1575 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1576 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1577 | |
Benjamin Kramer | 38f7f66 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1578 | // ~(A & ?) | A = -1 |
| 1579 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1580 | (A == Op1 || B == Op1)) |
| 1581 | return Constant::getAllOnesValue(Op1->getType()); |
| 1582 | |
| 1583 | // A | ~(A & ?) = -1 |
| 1584 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1585 | (A == Op0 || B == Op0)) |
| 1586 | return Constant::getAllOnesValue(Op0->getType()); |
| 1587 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1588 | // Try some generic simplifications for associative operations. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1589 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1590 | MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1591 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1592 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1593 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1594 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1595 | MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1596 | return V; |
| 1597 | |
| 1598 | // And distributes over Or. Try some generic simplifications based on this. |
| 1599 | if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1600 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1601 | return V; |
| 1602 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1603 | // If the operation is with the result of a select instruction, check whether |
| 1604 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1605 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1606 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1607 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1608 | return V; |
| 1609 | |
| 1610 | // If the operation is with the result of a phi instruction, check whether |
| 1611 | // 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] | 1612 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1613 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1614 | return V; |
| 1615 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1619 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1620 | const TargetLibraryInfo *TLI, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1621 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1622 | return ::SimplifyOrInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1623 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1624 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1625 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1626 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1627 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1628 | unsigned MaxRecurse) { |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1629 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1630 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1631 | Constant *Ops[] = { CLHS, CRHS }; |
| 1632 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1633 | Ops, Q.TD, Q.TLI); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | // Canonicalize the constant to the RHS. |
| 1637 | std::swap(Op0, Op1); |
| 1638 | } |
| 1639 | |
| 1640 | // A ^ undef -> undef |
Duncan Sands | f9e4a98 | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1641 | if (match(Op1, m_Undef())) |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1642 | return Op1; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1643 | |
| 1644 | // A ^ 0 = A |
| 1645 | if (match(Op1, m_Zero())) |
| 1646 | return Op0; |
| 1647 | |
Eli Friedman | f23d4ad | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1648 | // A ^ A = 0 |
| 1649 | if (Op0 == Op1) |
| 1650 | return Constant::getNullValue(Op0->getType()); |
| 1651 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1652 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1653 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1654 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1655 | return Constant::getAllOnesValue(Op0->getType()); |
| 1656 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1657 | // Try some generic simplifications for associative operations. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1658 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1659 | MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1660 | return V; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1661 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1662 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1663 | if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1664 | Q, MaxRecurse)) |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1665 | return V; |
| 1666 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1667 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1668 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1669 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1670 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1671 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1672 | // have been simplified to the common value of B and C already. Analysing |
| 1673 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1674 | // for threading over phi nodes. |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1675 | |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1679 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1680 | const TargetLibraryInfo *TLI, |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1681 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1682 | return ::SimplifyXorInst(Op0, Op1, Query (TD, TLI, DT), RecursionLimit); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1685 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1686 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1687 | } |
| 1688 | |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1689 | /// ExtractEquivalentCondition - Rummage around inside V looking for something |
| 1690 | /// equivalent to the comparison "LHS Pred RHS". Return such a value if found, |
| 1691 | /// otherwise return null. Helper function for analyzing max/min idioms. |
| 1692 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1693 | Value *LHS, Value *RHS) { |
| 1694 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1695 | if (!SI) |
| 1696 | return 0; |
| 1697 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1698 | if (!Cmp) |
| 1699 | return 0; |
| 1700 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1701 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1702 | return Cmp; |
| 1703 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1704 | LHS == CmpRHS && RHS == CmpLHS) |
| 1705 | return Cmp; |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
Dan Gohman | 901261d | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1709 | // A significant optimization not implemented here is assuming that alloca |
| 1710 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 1711 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 1712 | // conservative approach. |
| 1713 | // |
| 1714 | // This is inspired in part by C++11 5.10p1: |
| 1715 | // "Two pointers of the same type compare equal if and only if they are both |
| 1716 | // null, both point to the same function, or both represent the same |
| 1717 | // address." |
| 1718 | // |
| 1719 | // This is pretty permissive. |
| 1720 | // |
| 1721 | // It's also partly due to C11 6.5.9p6: |
| 1722 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 1723 | // pointers to the same object (including a pointer to an object and a |
| 1724 | // subobject at its beginning) or function, both are pointers to one past the |
| 1725 | // last element of the same array object, or one is a pointer to one past the |
| 1726 | // end of one array object and the other is a pointer to the start of a |
NAKAMURA Takumi | 92c3742 | 2013-04-08 23:05:21 +0000 | [diff] [blame] | 1727 | // different array object that happens to immediately follow the first array |
Dan Gohman | 901261d | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1728 | // object in the address space.) |
| 1729 | // |
| 1730 | // C11's version is more restrictive, however there's no reason why an argument |
| 1731 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 1732 | // equal to the beginning of a stack object in the callee. |
| 1733 | // |
| 1734 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 1735 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 1736 | // this optimization. |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 1737 | static Constant *computePointerICmp(const DataLayout *TD, |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1738 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1739 | CmpInst::Predicate Pred, |
| 1740 | Value *LHS, Value *RHS) { |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1741 | // First, skip past any trivial no-ops. |
| 1742 | LHS = LHS->stripPointerCasts(); |
| 1743 | RHS = RHS->stripPointerCasts(); |
| 1744 | |
| 1745 | // A non-null pointer is not equal to a null pointer. |
| 1746 | if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) && |
| 1747 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 1748 | return ConstantInt::get(GetCompareTy(LHS), |
| 1749 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1750 | |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1751 | // We can only fold certain predicates on pointer comparisons. |
| 1752 | switch (Pred) { |
| 1753 | default: |
| 1754 | return 0; |
| 1755 | |
| 1756 | // Equality comaprisons are easy to fold. |
| 1757 | case CmpInst::ICMP_EQ: |
| 1758 | case CmpInst::ICMP_NE: |
| 1759 | break; |
| 1760 | |
| 1761 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 1762 | // a GEP only protects against unsigned wrapping. |
| 1763 | case CmpInst::ICMP_UGT: |
| 1764 | case CmpInst::ICMP_UGE: |
| 1765 | case CmpInst::ICMP_ULT: |
| 1766 | case CmpInst::ICMP_ULE: |
| 1767 | // However, we have to switch them to their signed variants to handle |
| 1768 | // negative indices from the base pointer. |
| 1769 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 1770 | break; |
| 1771 | } |
| 1772 | |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1773 | // Strip off any constant offsets so that we can reason about them. |
| 1774 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 1775 | // here and compare base addresses like AliasAnalysis does, however there are |
| 1776 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 1777 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 1778 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1779 | Constant *LHSOffset = stripAndComputeConstantOffsets(TD, LHS); |
| 1780 | Constant *RHSOffset = stripAndComputeConstantOffsets(TD, RHS); |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1781 | |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1782 | // If LHS and RHS are related via constant offsets to the same base |
| 1783 | // value, we can replace it with an icmp which just compares the offsets. |
| 1784 | if (LHS == RHS) |
| 1785 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1786 | |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1787 | // Various optimizations for (in)equality comparisons. |
| 1788 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 1789 | // Different non-empty allocations that exist at the same time have |
| 1790 | // different addresses (if the program can tell). Global variables always |
| 1791 | // exist, so they always exist during the lifetime of each other and all |
| 1792 | // allocas. Two different allocas usually have different addresses... |
| 1793 | // |
| 1794 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 1795 | // allocas, they may have the same address. It's tempting to reduce the |
| 1796 | // scope of the problem by only looking at *static* allocas here. That would |
| 1797 | // cover the majority of allocas while significantly reducing the likelihood |
| 1798 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 1799 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 1800 | // an entry block. Also, if we have a block that's not attached to a |
| 1801 | // function, we can't tell if it's "static" under the current definition. |
| 1802 | // Theoretically, this problem could be fixed by creating a new kind of |
| 1803 | // instruction kind specifically for static allocas. Such a new instruction |
| 1804 | // could be required to be at the top of the entry block, thus preventing it |
| 1805 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 1806 | // convert regular allocas into these special allocas. It'd be nifty. |
| 1807 | // However, until then, this problem remains open. |
| 1808 | // |
| 1809 | // So, we'll assume that two non-empty allocas have different addresses |
| 1810 | // for now. |
| 1811 | // |
| 1812 | // With all that, if the offsets are within the bounds of their allocations |
| 1813 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 1814 | // allocations aren't the same, the pointers are not equal. |
| 1815 | // |
| 1816 | // Note that it's not necessary to check for LHS being a global variable |
| 1817 | // address, due to canonicalization and constant folding. |
| 1818 | if (isa<AllocaInst>(LHS) && |
| 1819 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1820 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 1821 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1822 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1823 | if (LHSOffsetCI && RHSOffsetCI && |
| 1824 | getObjectSize(LHS, LHSSize, TD, TLI) && |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1825 | getObjectSize(RHS, RHSSize, TD, TLI)) { |
Benjamin Kramer | d9f32c2 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1826 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 1827 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1828 | if (!LHSOffsetValue.isNegative() && |
| 1829 | !RHSOffsetValue.isNegative() && |
| 1830 | LHSOffsetValue.ult(LHSSize) && |
| 1831 | RHSOffsetValue.ult(RHSSize)) { |
| 1832 | return ConstantInt::get(GetCompareTy(LHS), |
| 1833 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | // Repeat the above check but this time without depending on DataLayout |
| 1838 | // or being able to compute a precise size. |
| 1839 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 1840 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 1841 | LHSOffset->isNullValue() && |
| 1842 | RHSOffset->isNullValue()) |
| 1843 | return ConstantInt::get(GetCompareTy(LHS), |
| 1844 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | // Otherwise, fail. |
| 1849 | return 0; |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1850 | } |
Chris Lattner | 009e265 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1852 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1853 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1854 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1855 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1856 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1857 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1859 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1860 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1861 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.TD, Q.TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1862 | |
| 1863 | // If we have a constant, make sure it is on the RHS. |
| 1864 | std::swap(LHS, RHS); |
| 1865 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1866 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1868 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 1869 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1871 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1872 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1873 | // because X could be 0. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1874 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1875 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1876 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1877 | // Special case logic when the operands have i1 type. |
Nick Lewycky | 66d004e | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 1878 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1879 | switch (Pred) { |
| 1880 | default: break; |
| 1881 | case ICmpInst::ICMP_EQ: |
| 1882 | // X == 1 -> X |
| 1883 | if (match(RHS, m_One())) |
| 1884 | return LHS; |
| 1885 | break; |
| 1886 | case ICmpInst::ICMP_NE: |
| 1887 | // X != 0 -> X |
| 1888 | if (match(RHS, m_Zero())) |
| 1889 | return LHS; |
| 1890 | break; |
| 1891 | case ICmpInst::ICMP_UGT: |
| 1892 | // X >u 0 -> X |
| 1893 | if (match(RHS, m_Zero())) |
| 1894 | return LHS; |
| 1895 | break; |
| 1896 | case ICmpInst::ICMP_UGE: |
| 1897 | // X >=u 1 -> X |
| 1898 | if (match(RHS, m_One())) |
| 1899 | return LHS; |
| 1900 | break; |
| 1901 | case ICmpInst::ICMP_SLT: |
| 1902 | // X <s 0 -> X |
| 1903 | if (match(RHS, m_Zero())) |
| 1904 | return LHS; |
| 1905 | break; |
| 1906 | case ICmpInst::ICMP_SLE: |
| 1907 | // X <=s -1 -> X |
| 1908 | if (match(RHS, m_One())) |
| 1909 | return LHS; |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1914 | // If we are comparing with zero then try hard since this is a common case. |
| 1915 | if (match(RHS, m_Zero())) { |
| 1916 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 1917 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1918 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1919 | case ICmpInst::ICMP_ULT: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1920 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1921 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1922 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1923 | case ICmpInst::ICMP_EQ: |
| 1924 | case ICmpInst::ICMP_ULE: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1925 | if (isKnownNonZero(LHS, Q.TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1926 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1927 | break; |
| 1928 | case ICmpInst::ICMP_NE: |
| 1929 | case ICmpInst::ICMP_UGT: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1930 | if (isKnownNonZero(LHS, Q.TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1931 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1932 | break; |
| 1933 | case ICmpInst::ICMP_SLT: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1934 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1935 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1936 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1937 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1938 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | case ICmpInst::ICMP_SLE: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1941 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1942 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1943 | return getTrue(ITy); |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1944 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1945 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1946 | break; |
| 1947 | case ICmpInst::ICMP_SGE: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1948 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1949 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1950 | return getFalse(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1951 | if (LHSKnownNonNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1952 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | case ICmpInst::ICMP_SGT: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1955 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.TD); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1956 | if (LHSKnownNegative) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1957 | return getFalse(ITy); |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1958 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.TD)) |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1959 | return getTrue(ITy); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1965 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1966 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
| 1967 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue()); |
| 1968 | if (RHS_CR.isEmptySet()) |
| 1969 | return ConstantInt::getFalse(CI->getContext()); |
| 1970 | if (RHS_CR.isFullSet()) |
| 1971 | return ConstantInt::getTrue(CI->getContext()); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1972 | |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1973 | // Many binary operators with constant RHS have easy to compute constant |
| 1974 | // range. Use them to check whether the comparison is a tautology. |
| 1975 | uint32_t Width = CI->getBitWidth(); |
| 1976 | APInt Lower = APInt(Width, 0); |
| 1977 | APInt Upper = APInt(Width, 0); |
| 1978 | ConstantInt *CI2; |
| 1979 | if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) { |
| 1980 | // 'urem x, CI2' produces [0, CI2). |
| 1981 | Upper = CI2->getValue(); |
| 1982 | } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) { |
| 1983 | // 'srem x, CI2' produces (-|CI2|, |CI2|). |
| 1984 | Upper = CI2->getValue().abs(); |
| 1985 | Lower = (-Upper) + 1; |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 1986 | } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) { |
| 1987 | // 'udiv CI2, x' produces [0, CI2]. |
Eli Friedman | 7781ae5 | 2011-11-08 21:08:02 +0000 | [diff] [blame] | 1988 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3a73e34 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1989 | } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1990 | // 'udiv x, CI2' produces [0, UINT_MAX / CI2]. |
| 1991 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1992 | if (!CI2->isZero()) |
| 1993 | Upper = NegOne.udiv(CI2->getValue()) + 1; |
| 1994 | } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1995 | // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2]. |
| 1996 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1997 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 1998 | APInt Val = CI2->getValue().abs(); |
| 1999 | if (!Val.isMinValue()) { |
| 2000 | Lower = IntMin.sdiv(Val); |
| 2001 | Upper = IntMax.sdiv(Val) + 1; |
| 2002 | } |
| 2003 | } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) { |
| 2004 | // 'lshr x, CI2' produces [0, UINT_MAX >> CI2]. |
| 2005 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 2006 | if (CI2->getValue().ult(Width)) |
| 2007 | Upper = NegOne.lshr(CI2->getValue()) + 1; |
| 2008 | } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) { |
| 2009 | // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2]. |
| 2010 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2011 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2012 | if (CI2->getValue().ult(Width)) { |
| 2013 | Lower = IntMin.ashr(CI2->getValue()); |
| 2014 | Upper = IntMax.ashr(CI2->getValue()) + 1; |
| 2015 | } |
| 2016 | } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) { |
| 2017 | // 'or x, CI2' produces [CI2, UINT_MAX]. |
| 2018 | Lower = CI2->getValue(); |
| 2019 | } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) { |
| 2020 | // 'and x, CI2' produces [0, CI2]. |
| 2021 | Upper = CI2->getValue() + 1; |
| 2022 | } |
| 2023 | if (Lower != Upper) { |
| 2024 | ConstantRange LHS_CR = ConstantRange(Lower, Upper); |
| 2025 | if (RHS_CR.contains(LHS_CR)) |
| 2026 | return ConstantInt::getTrue(RHS->getContext()); |
| 2027 | if (RHS_CR.inverse().contains(LHS_CR)) |
| 2028 | return ConstantInt::getFalse(RHS->getContext()); |
| 2029 | } |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2032 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 2033 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 2034 | Instruction *LI = cast<CastInst>(LHS); |
| 2035 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2036 | Type *SrcTy = SrcOp->getType(); |
| 2037 | Type *DstTy = LI->getType(); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2038 | |
| 2039 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 2040 | // if the integer type is the same size as the pointer type. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2041 | if (MaxRecurse && Q.TD && isa<PtrToIntInst>(LI) && |
Matt Arsenault | 7eef3bd | 2013-08-02 00:10:44 +0000 | [diff] [blame] | 2042 | Q.TD->getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2043 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2044 | // Transfer the cast to the constant. |
| 2045 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 2046 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2047 | Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2048 | return V; |
| 2049 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 2050 | if (RI->getOperand(0)->getType() == SrcTy) |
| 2051 | // Compare without the cast. |
| 2052 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2053 | Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2054 | return V; |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | if (isa<ZExtInst>(LHS)) { |
| 2059 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 2060 | // same type. |
| 2061 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 2062 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2063 | // Compare X and Y. Note that signed predicates become unsigned. |
| 2064 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2065 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2066 | MaxRecurse-1)) |
| 2067 | return V; |
| 2068 | } |
| 2069 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 2070 | // too. If not, then try to deduce the result of the comparison. |
| 2071 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2072 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2073 | // reextended to DstTy. |
| 2074 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2075 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 2076 | |
| 2077 | // If the re-extended constant didn't change then this is effectively |
| 2078 | // also a case of comparing two zero-extended values. |
| 2079 | if (RExt == CI && MaxRecurse) |
| 2080 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2081 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2082 | return V; |
| 2083 | |
| 2084 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 2085 | // there. Use this to work out the result of the comparison. |
| 2086 | if (RExt != CI) { |
| 2087 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2088 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2089 | // LHS <u RHS. |
| 2090 | case ICmpInst::ICMP_EQ: |
| 2091 | case ICmpInst::ICMP_UGT: |
| 2092 | case ICmpInst::ICMP_UGE: |
| 2093 | return ConstantInt::getFalse(CI->getContext()); |
| 2094 | |
| 2095 | case ICmpInst::ICMP_NE: |
| 2096 | case ICmpInst::ICMP_ULT: |
| 2097 | case ICmpInst::ICMP_ULE: |
| 2098 | return ConstantInt::getTrue(CI->getContext()); |
| 2099 | |
| 2100 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 2101 | // is non-negative then LHS <s RHS. |
| 2102 | case ICmpInst::ICMP_SGT: |
| 2103 | case ICmpInst::ICMP_SGE: |
| 2104 | return CI->getValue().isNegative() ? |
| 2105 | ConstantInt::getTrue(CI->getContext()) : |
| 2106 | ConstantInt::getFalse(CI->getContext()); |
| 2107 | |
| 2108 | case ICmpInst::ICMP_SLT: |
| 2109 | case ICmpInst::ICMP_SLE: |
| 2110 | return CI->getValue().isNegative() ? |
| 2111 | ConstantInt::getFalse(CI->getContext()) : |
| 2112 | ConstantInt::getTrue(CI->getContext()); |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | if (isa<SExtInst>(LHS)) { |
| 2119 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 2120 | // same type. |
| 2121 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 2122 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2123 | // Compare X and Y. Note that the predicate does not change. |
| 2124 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2125 | Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2126 | return V; |
| 2127 | } |
| 2128 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 2129 | // too. If not, then try to deduce the result of the comparison. |
| 2130 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2131 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2132 | // reextended to DstTy. |
| 2133 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2134 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 2135 | |
| 2136 | // If the re-extended constant didn't change then this is effectively |
| 2137 | // also a case of comparing two sign-extended values. |
| 2138 | if (RExt == CI && MaxRecurse) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2139 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2140 | return V; |
| 2141 | |
| 2142 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 2143 | // bits there. Use this to work out the result of the comparison. |
| 2144 | if (RExt != CI) { |
| 2145 | switch (Pred) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2146 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2147 | case ICmpInst::ICMP_EQ: |
| 2148 | return ConstantInt::getFalse(CI->getContext()); |
| 2149 | case ICmpInst::ICMP_NE: |
| 2150 | return ConstantInt::getTrue(CI->getContext()); |
| 2151 | |
| 2152 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 2153 | // LHS >s RHS. |
| 2154 | case ICmpInst::ICMP_SGT: |
| 2155 | case ICmpInst::ICMP_SGE: |
| 2156 | return CI->getValue().isNegative() ? |
| 2157 | ConstantInt::getTrue(CI->getContext()) : |
| 2158 | ConstantInt::getFalse(CI->getContext()); |
| 2159 | case ICmpInst::ICMP_SLT: |
| 2160 | case ICmpInst::ICMP_SLE: |
| 2161 | return CI->getValue().isNegative() ? |
| 2162 | ConstantInt::getFalse(CI->getContext()) : |
| 2163 | ConstantInt::getTrue(CI->getContext()); |
| 2164 | |
| 2165 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 2166 | // LHS >u RHS. |
| 2167 | case ICmpInst::ICMP_UGT: |
| 2168 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2169 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2170 | if (MaxRecurse) |
| 2171 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 2172 | Constant::getNullValue(SrcTy), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2173 | Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2174 | return V; |
| 2175 | break; |
| 2176 | case ICmpInst::ICMP_ULT: |
| 2177 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2178 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2179 | if (MaxRecurse) |
| 2180 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 2181 | Constant::getNullValue(SrcTy), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2182 | Q, MaxRecurse-1)) |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2183 | return V; |
| 2184 | break; |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | } |
| 2190 | |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2191 | // Special logic for binary operators. |
| 2192 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2193 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2194 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2195 | // Analyze the case when either LHS or RHS is an add instruction. |
| 2196 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 2197 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2198 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2199 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2200 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 2201 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2202 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2203 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2204 | } |
| 2205 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2206 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 2207 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2208 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2209 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2210 | } |
| 2211 | |
| 2212 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2213 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2214 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2215 | Constant::getNullValue(RHS->getType()), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2216 | Q, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2217 | return V; |
| 2218 | |
| 2219 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2220 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2221 | if (Value *V = SimplifyICmpInst(Pred, |
| 2222 | Constant::getNullValue(LHS->getType()), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2223 | C == LHS ? D : C, Q, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2224 | return V; |
| 2225 | |
| 2226 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2227 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 2228 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 2229 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
Duncan Sands | aceb03e | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2230 | Value *Y, *Z; |
| 2231 | if (A == C) { |
Duncan Sands | 4f0dfbb | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2232 | // C + B == C + D -> B == D |
Duncan Sands | aceb03e | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2233 | Y = B; |
| 2234 | Z = D; |
| 2235 | } else if (A == D) { |
Duncan Sands | 4f0dfbb | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2236 | // D + B == C + D -> B == C |
Duncan Sands | aceb03e | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2237 | Y = B; |
| 2238 | Z = C; |
| 2239 | } else if (B == C) { |
Duncan Sands | 4f0dfbb | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2240 | // A + C == C + D -> A == D |
Duncan Sands | aceb03e | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2241 | Y = A; |
| 2242 | Z = D; |
Duncan Sands | 4f0dfbb | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2243 | } else { |
| 2244 | assert(B == D); |
| 2245 | // A + D == C + D -> A == C |
Duncan Sands | aceb03e | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2246 | Y = A; |
| 2247 | Z = C; |
| 2248 | } |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2249 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 52fb846 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2250 | return V; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2254 | // icmp pred (urem X, Y), Y |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2255 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2256 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2257 | switch (Pred) { |
| 2258 | default: |
| 2259 | break; |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2260 | case ICmpInst::ICMP_SGT: |
| 2261 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2262 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.TD); |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2263 | if (!KnownNonNegative) |
| 2264 | break; |
| 2265 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2266 | case ICmpInst::ICMP_EQ: |
| 2267 | case ICmpInst::ICMP_UGT: |
| 2268 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2269 | return getFalse(ITy); |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2270 | case ICmpInst::ICMP_SLT: |
| 2271 | case ICmpInst::ICMP_SLE: |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2272 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.TD); |
Nick Lewycky | 7867927 | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2273 | if (!KnownNonNegative) |
| 2274 | break; |
| 2275 | // fall-through |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2276 | case ICmpInst::ICMP_NE: |
| 2277 | case ICmpInst::ICMP_ULT: |
| 2278 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2279 | return getTrue(ITy); |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2280 | } |
| 2281 | } |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2282 | |
| 2283 | // icmp pred X, (urem Y, X) |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2284 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2285 | bool KnownNonNegative, KnownNegative; |
| 2286 | switch (Pred) { |
| 2287 | default: |
| 2288 | break; |
| 2289 | case ICmpInst::ICMP_SGT: |
| 2290 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2291 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.TD); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2292 | if (!KnownNonNegative) |
| 2293 | break; |
| 2294 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2295 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2296 | case ICmpInst::ICMP_UGT: |
| 2297 | case ICmpInst::ICMP_UGE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2298 | return getTrue(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2299 | case ICmpInst::ICMP_SLT: |
| 2300 | case ICmpInst::ICMP_SLE: |
Nick Lewycky | 8a23270 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2301 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.TD); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2302 | if (!KnownNonNegative) |
| 2303 | break; |
| 2304 | // fall-through |
Nick Lewycky | a0e2f38 | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2305 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2306 | case ICmpInst::ICMP_ULT: |
| 2307 | case ICmpInst::ICMP_ULE: |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2308 | return getFalse(ITy); |
Nick Lewycky | 84dd4fa | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
Nick Lewycky | 88cd0aa | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2311 | |
Duncan Sands | c65c747 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2312 | // x udiv y <=u x. |
| 2313 | if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { |
| 2314 | // icmp pred (X /u Y), X |
| 2315 | if (Pred == ICmpInst::ICMP_UGT) |
| 2316 | return getFalse(ITy); |
| 2317 | if (Pred == ICmpInst::ICMP_ULE) |
| 2318 | return getTrue(ITy); |
| 2319 | } |
| 2320 | |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2321 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2322 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2323 | switch (LBO->getOpcode()) { |
| 2324 | default: break; |
| 2325 | case Instruction::UDiv: |
| 2326 | case Instruction::LShr: |
| 2327 | if (ICmpInst::isSigned(Pred)) |
| 2328 | break; |
| 2329 | // fall-through |
| 2330 | case Instruction::SDiv: |
| 2331 | case Instruction::AShr: |
Eli Friedman | b6e7cd6 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2332 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2333 | break; |
| 2334 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2335 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2336 | return V; |
| 2337 | break; |
| 2338 | case Instruction::Shl: { |
Duncan Sands | c9d904e | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2339 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2340 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2341 | if (!NUW && !NSW) |
| 2342 | break; |
| 2343 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2344 | break; |
| 2345 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2346 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2347 | return V; |
| 2348 | break; |
| 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2353 | // Simplify comparisons involving max/min. |
| 2354 | Value *A, *B; |
| 2355 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2356 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2357 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2358 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2359 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2360 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2361 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2362 | // We analyze this as smax(A, B) pred A. |
| 2363 | P = Pred; |
| 2364 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2365 | (A == LHS || B == LHS)) { |
| 2366 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2367 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2368 | // We analyze this as smax(A, B) swapped-pred A. |
| 2369 | P = CmpInst::getSwappedPredicate(Pred); |
| 2370 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2371 | (A == RHS || B == RHS)) { |
| 2372 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2373 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2374 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2375 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2376 | P = CmpInst::getSwappedPredicate(Pred); |
| 2377 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2378 | (A == LHS || B == LHS)) { |
| 2379 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2380 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2381 | // We analyze this as smax(-A, -B) pred -A. |
| 2382 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2383 | P = Pred; |
| 2384 | } |
| 2385 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2386 | // Cases correspond to "max(A, B) p A". |
| 2387 | switch (P) { |
| 2388 | default: |
| 2389 | break; |
| 2390 | case CmpInst::ICMP_EQ: |
| 2391 | case CmpInst::ICMP_SLE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2392 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2393 | // in the max/min; if so, we can just return that. |
| 2394 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2395 | return V; |
| 2396 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2397 | return V; |
| 2398 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2399 | if (MaxRecurse) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2400 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2401 | return V; |
| 2402 | break; |
| 2403 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2404 | case CmpInst::ICMP_SGT: { |
| 2405 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2406 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2407 | // tested in the max/min; if so, we can just return that. |
| 2408 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2409 | return V; |
| 2410 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2411 | return V; |
| 2412 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2413 | if (MaxRecurse) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2414 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2415 | return V; |
| 2416 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2417 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2418 | case CmpInst::ICMP_SGE: |
| 2419 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2420 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2421 | case CmpInst::ICMP_SLT: |
| 2422 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2423 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2424 | } |
| 2425 | } |
| 2426 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2427 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2428 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2429 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2430 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2431 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2432 | // We analyze this as umax(A, B) pred A. |
| 2433 | P = Pred; |
| 2434 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2435 | (A == LHS || B == LHS)) { |
| 2436 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2437 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2438 | // We analyze this as umax(A, B) swapped-pred A. |
| 2439 | P = CmpInst::getSwappedPredicate(Pred); |
| 2440 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2441 | (A == RHS || B == RHS)) { |
| 2442 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2443 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2444 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2445 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2446 | P = CmpInst::getSwappedPredicate(Pred); |
| 2447 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2448 | (A == LHS || B == LHS)) { |
| 2449 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2450 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2451 | // We analyze this as umax(-A, -B) pred -A. |
| 2452 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2453 | P = Pred; |
| 2454 | } |
| 2455 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2456 | // Cases correspond to "max(A, B) p A". |
| 2457 | switch (P) { |
| 2458 | default: |
| 2459 | break; |
| 2460 | case CmpInst::ICMP_EQ: |
| 2461 | case CmpInst::ICMP_ULE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2462 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2463 | // in the max/min; if so, we can just return that. |
| 2464 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2465 | return V; |
| 2466 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2467 | return V; |
| 2468 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2469 | if (MaxRecurse) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2470 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2471 | return V; |
| 2472 | break; |
| 2473 | case CmpInst::ICMP_NE: |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2474 | case CmpInst::ICMP_UGT: { |
| 2475 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2476 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2477 | // tested in the max/min; if so, we can just return that. |
| 2478 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2479 | return V; |
| 2480 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2481 | return V; |
| 2482 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2483 | if (MaxRecurse) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2484 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2485 | return V; |
| 2486 | break; |
Duncan Sands | e864b5b | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2487 | } |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2488 | case CmpInst::ICMP_UGE: |
| 2489 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2490 | return getTrue(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2491 | case CmpInst::ICMP_ULT: |
| 2492 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2493 | return getFalse(ITy); |
Duncan Sands | ad20681 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2497 | // Variants on "max(x,y) >= min(x,z)". |
| 2498 | Value *C, *D; |
| 2499 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2500 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 2501 | (A == C || A == D || B == C || B == D)) { |
| 2502 | // max(x, ?) pred min(x, ?). |
| 2503 | if (Pred == CmpInst::ICMP_SGE) |
| 2504 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2505 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2506 | if (Pred == CmpInst::ICMP_SLT) |
| 2507 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2508 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2509 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2510 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 2511 | (A == C || A == D || B == C || B == D)) { |
| 2512 | // min(x, ?) pred max(x, ?). |
| 2513 | if (Pred == CmpInst::ICMP_SLE) |
| 2514 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2515 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2516 | if (Pred == CmpInst::ICMP_SGT) |
| 2517 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2518 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2519 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2520 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 2521 | (A == C || A == D || B == C || B == D)) { |
| 2522 | // max(x, ?) pred min(x, ?). |
| 2523 | if (Pred == CmpInst::ICMP_UGE) |
| 2524 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2525 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2526 | if (Pred == CmpInst::ICMP_ULT) |
| 2527 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2528 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2529 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2530 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 2531 | (A == C || A == D || B == C || B == D)) { |
| 2532 | // min(x, ?) pred max(x, ?). |
| 2533 | if (Pred == CmpInst::ICMP_ULE) |
| 2534 | // Always true. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2535 | return getTrue(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2536 | if (Pred == CmpInst::ICMP_UGT) |
| 2537 | // Always false. |
Duncan Sands | f56138d | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2538 | return getFalse(ITy); |
Duncan Sands | 8140ad3 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2541 | // Simplify comparisons of related pointers using a powerful, recursive |
| 2542 | // GEP-walk when we have target data available.. |
Dan Gohman | 3e3de56 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 2543 | if (LHS->getType()->isPointerTy()) |
Dan Gohman | fdd1eaf | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2544 | if (Constant *C = computePointerICmp(Q.TD, Q.TLI, Pred, LHS, RHS)) |
Chandler Carruth | 58725a6 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2545 | return C; |
| 2546 | |
Nick Lewycky | f7087ea | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 2547 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 2548 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 2549 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 2550 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 2551 | (ICmpInst::isEquality(Pred) || |
| 2552 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 2553 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 2554 | // The bases are equal and the indices are constant. Build a constant |
| 2555 | // expression GEP with the same indices and a null base pointer to see |
| 2556 | // what constant folding can make out of it. |
| 2557 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 2558 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
| 2559 | Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); |
| 2560 | |
| 2561 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
| 2562 | Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS); |
| 2563 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2568 | // If the comparison is with the result of a select instruction, check whether |
| 2569 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2570 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2571 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2572 | return V; |
| 2573 | |
| 2574 | // If the comparison is with the result of a phi instruction, check whether |
| 2575 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2576 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2577 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2578 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2579 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2580 | return 0; |
| 2581 | } |
| 2582 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2583 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2584 | const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2585 | const TargetLibraryInfo *TLI, |
| 2586 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2587 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT), |
| 2588 | RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2591 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 2592 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2593 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2594 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2595 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 2596 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 2597 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2598 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2599 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2600 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.TD, Q.TLI); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2601 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2602 | // If we have a constant, make sure it is on the RHS. |
| 2603 | std::swap(LHS, RHS); |
| 2604 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2605 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2607 | // Fold trivial predicates. |
| 2608 | if (Pred == FCmpInst::FCMP_FALSE) |
| 2609 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2610 | if (Pred == FCmpInst::FCMP_TRUE) |
| 2611 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2612 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2613 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 2614 | return UndefValue::get(GetCompareTy(LHS)); |
| 2615 | |
| 2616 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2617 | if (LHS == RHS) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2618 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 2619 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2620 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 2621 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2622 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2624 | // Handle fcmp with constant RHS |
| 2625 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2626 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 2627 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 2628 | if (CFP->getValueAPF().isNaN()) { |
| 2629 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 2630 | return ConstantInt::getFalse(CFP->getContext()); |
| 2631 | assert(FCmpInst::isUnordered(Pred) && |
| 2632 | "Comparison must be either ordered or unordered!"); |
| 2633 | // True if unordered. |
| 2634 | return ConstantInt::getTrue(CFP->getContext()); |
| 2635 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 2636 | // Check whether the constant is an infinity. |
| 2637 | if (CFP->getValueAPF().isInfinity()) { |
| 2638 | if (CFP->getValueAPF().isNegative()) { |
| 2639 | switch (Pred) { |
| 2640 | case FCmpInst::FCMP_OLT: |
| 2641 | // No value is ordered and less than negative infinity. |
| 2642 | return ConstantInt::getFalse(CFP->getContext()); |
| 2643 | case FCmpInst::FCMP_UGE: |
| 2644 | // All values are unordered with or at least negative infinity. |
| 2645 | return ConstantInt::getTrue(CFP->getContext()); |
| 2646 | default: |
| 2647 | break; |
| 2648 | } |
| 2649 | } else { |
| 2650 | switch (Pred) { |
| 2651 | case FCmpInst::FCMP_OGT: |
| 2652 | // No value is ordered and greater than infinity. |
| 2653 | return ConstantInt::getFalse(CFP->getContext()); |
| 2654 | case FCmpInst::FCMP_ULE: |
| 2655 | // All values are unordered with and at most infinity. |
| 2656 | return ConstantInt::getTrue(CFP->getContext()); |
| 2657 | default: |
| 2658 | break; |
| 2659 | } |
| 2660 | } |
| 2661 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2662 | } |
| 2663 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2664 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2665 | // If the comparison is with the result of a select instruction, check whether |
| 2666 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2667 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2668 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2669 | return V; |
| 2670 | |
| 2671 | // If the comparison is with the result of a phi instruction, check whether |
| 2672 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2673 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2674 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2675 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2677 | return 0; |
| 2678 | } |
| 2679 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2680 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2681 | const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2682 | const TargetLibraryInfo *TLI, |
| 2683 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2684 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT), |
| 2685 | RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2688 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 2689 | /// the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2690 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 2691 | Value *FalseVal, const Query &Q, |
| 2692 | unsigned MaxRecurse) { |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2693 | // select true, X, Y -> X |
| 2694 | // select false, X, Y -> Y |
| 2695 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 2696 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2698 | // select C, X, X -> X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2699 | if (TrueVal == FalseVal) |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2700 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2702 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 2703 | if (isa<Constant>(TrueVal)) |
| 2704 | return TrueVal; |
| 2705 | return FalseVal; |
| 2706 | } |
Dan Gohman | 68c0dbc | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 2707 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 2708 | return FalseVal; |
| 2709 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 2710 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2712 | return 0; |
| 2713 | } |
| 2714 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2715 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2716 | const DataLayout *TD, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2717 | const TargetLibraryInfo *TLI, |
| 2718 | const DominatorTree *DT) { |
| 2719 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Query (TD, TLI, DT), |
| 2720 | RecursionLimit); |
| 2721 | } |
| 2722 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2723 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 2724 | /// fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2725 | static Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const Query &Q, unsigned) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2726 | // The type of the GEP pointer operand. |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2727 | PointerType *PtrTy = dyn_cast<PointerType>(Ops[0]->getType()); |
| 2728 | // The GEP pointer operand is not a pointer, it's a vector of pointers. |
| 2729 | if (!PtrTy) |
| 2730 | return 0; |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2731 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2732 | // getelementptr P -> P. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2733 | if (Ops.size() == 1) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2734 | return Ops[0]; |
| 2735 | |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2736 | if (isa<UndefValue>(Ops[0])) { |
| 2737 | // Compute the (pointer) type returned by the GEP instruction. |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2738 | Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2739 | Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2740 | return UndefValue::get(GEPTy); |
| 2741 | } |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2742 | |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2743 | if (Ops.size() == 2) { |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2744 | // getelementptr P, 0 -> P. |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2745 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 2746 | if (C->isZero()) |
| 2747 | return Ops[0]; |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2748 | // 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] | 2749 | if (Q.TD) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2750 | Type *Ty = PtrTy->getElementType(); |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2751 | if (Ty->isSized() && Q.TD->getTypeAllocSize(Ty) == 0) |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2752 | return Ops[0]; |
| 2753 | } |
| 2754 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2756 | // Check to see if this is constant foldable. |
Jay Foad | b9b54eb | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2757 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2758 | if (!isa<Constant>(Ops[i])) |
| 2759 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2760 | |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2761 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1)); |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2764 | Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2765 | const TargetLibraryInfo *TLI, |
| 2766 | const DominatorTree *DT) { |
| 2767 | return ::SimplifyGEPInst(Ops, Query (TD, TLI, DT), RecursionLimit); |
| 2768 | } |
| 2769 | |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2770 | /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we |
| 2771 | /// can fold the result. If not, this returns null. |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2772 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2773 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 2774 | unsigned) { |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2775 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 2776 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 2777 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 2778 | |
| 2779 | // insertvalue x, undef, n -> x |
| 2780 | if (match(Val, m_Undef())) |
| 2781 | return Agg; |
| 2782 | |
| 2783 | // insertvalue x, (extractvalue y, n), n |
| 2784 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | ae707bd | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 2785 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 2786 | EV->getIndices() == Idxs) { |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2787 | // insertvalue undef, (extractvalue y, n), n -> y |
| 2788 | if (match(Agg, m_Undef())) |
| 2789 | return EV->getAggregateOperand(); |
| 2790 | |
| 2791 | // insertvalue y, (extractvalue y, n), n -> y |
| 2792 | if (Agg == EV->getAggregateOperand()) |
| 2793 | return Agg; |
| 2794 | } |
| 2795 | |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2799 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2800 | ArrayRef<unsigned> Idxs, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2801 | const DataLayout *TD, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2802 | const TargetLibraryInfo *TLI, |
| 2803 | const DominatorTree *DT) { |
| 2804 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query (TD, TLI, DT), |
| 2805 | RecursionLimit); |
| 2806 | } |
| 2807 | |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2808 | /// 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] | 2809 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2810 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 2811 | // with the common value. |
| 2812 | Value *CommonValue = 0; |
| 2813 | bool HasUndefInput = false; |
| 2814 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2815 | Value *Incoming = PN->getIncomingValue(i); |
| 2816 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 2817 | if (Incoming == PN) continue; |
| 2818 | if (isa<UndefValue>(Incoming)) { |
| 2819 | // Remember that we saw an undef value, but otherwise ignore them. |
| 2820 | HasUndefInput = true; |
| 2821 | continue; |
| 2822 | } |
| 2823 | if (CommonValue && Incoming != CommonValue) |
| 2824 | return 0; // Not the same, bail out. |
| 2825 | CommonValue = Incoming; |
| 2826 | } |
| 2827 | |
| 2828 | // If CommonValue is null then all of the incoming values were either undef or |
| 2829 | // equal to the phi node itself. |
| 2830 | if (!CommonValue) |
| 2831 | return UndefValue::get(PN->getType()); |
| 2832 | |
| 2833 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 2834 | // instruction, we cannot return X as the result of the PHI node unless it |
| 2835 | // dominates the PHI block. |
| 2836 | if (HasUndefInput) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2837 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : 0; |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2838 | |
| 2839 | return CommonValue; |
| 2840 | } |
| 2841 | |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2842 | static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) { |
| 2843 | if (Constant *C = dyn_cast<Constant>(Op)) |
| 2844 | return ConstantFoldInstOperands(Instruction::Trunc, Ty, C, Q.TD, Q.TLI); |
| 2845 | |
| 2846 | return 0; |
| 2847 | } |
| 2848 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2849 | Value *llvm::SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD, |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2850 | const TargetLibraryInfo *TLI, |
| 2851 | const DominatorTree *DT) { |
| 2852 | return ::SimplifyTruncInst(Op, Ty, Query (TD, TLI, DT), RecursionLimit); |
| 2853 | } |
| 2854 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2855 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2856 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2857 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 2858 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2859 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2860 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2861 | switch (Opcode) { |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2862 | case Instruction::Add: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2863 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2864 | Q, MaxRecurse); |
Michael Ilseman | d0a0d22 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2865 | case Instruction::FAdd: |
| 2866 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 2867 | |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2868 | case Instruction::Sub: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2869 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2870 | Q, MaxRecurse); |
Michael Ilseman | d0a0d22 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2871 | case Instruction::FSub: |
| 2872 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 2873 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2874 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d0a0d22 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2875 | case Instruction::FMul: |
| 2876 | return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2877 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 2878 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
| 2879 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, Q, MaxRecurse); |
| 2880 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 2881 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
| 2882 | case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2883 | case Instruction::Shl: |
Duncan Sands | ffeb98a | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2884 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2885 | Q, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2886 | case Instruction::LShr: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2887 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2888 | case Instruction::AShr: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2889 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
| 2890 | case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 2891 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, Q, MaxRecurse); |
| 2892 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2893 | default: |
| 2894 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 2895 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 2896 | Constant *COps[] = {CLHS, CRHS}; |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2897 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, Q.TD, |
| 2898 | Q.TLI); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2899 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2900 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2901 | // If the operation is associative, try some generic simplifications. |
| 2902 | if (Instruction::isAssociative(Opcode)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2903 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2904 | return V; |
| 2905 | |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2906 | // 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] | 2907 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2908 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2909 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2910 | return V; |
| 2911 | |
| 2912 | // If the operation is with the result of a phi instruction, check whether |
| 2913 | // 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] | 2914 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2915 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2916 | return V; |
| 2917 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2918 | return 0; |
| 2919 | } |
| 2920 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2921 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2922 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2923 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2924 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2925 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query (TD, TLI, DT), RecursionLimit); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2928 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 2929 | /// fold the result. |
| 2930 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2931 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2932 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2933 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
| 2934 | return SimplifyFCmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2938 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2939 | const DominatorTree *DT) { |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2940 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query (TD, TLI, DT), |
| 2941 | RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2942 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2943 | |
Michael Ilseman | f89de81 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 2944 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 2945 | switch (ID) { |
| 2946 | default: return false; |
| 2947 | |
| 2948 | // Unary idempotent: f(f(x)) = f(x) |
| 2949 | case Intrinsic::fabs: |
| 2950 | case Intrinsic::floor: |
| 2951 | case Intrinsic::ceil: |
| 2952 | case Intrinsic::trunc: |
| 2953 | case Intrinsic::rint: |
| 2954 | case Intrinsic::nearbyint: |
| 2955 | return true; |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | template <typename IterTy> |
| 2960 | static Value *SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd, |
| 2961 | const Query &Q, unsigned MaxRecurse) { |
| 2962 | // Perform idempotent optimizations |
| 2963 | if (!IsIdempotent(IID)) |
| 2964 | return 0; |
| 2965 | |
| 2966 | // Unary Ops |
| 2967 | if (std::distance(ArgBegin, ArgEnd) == 1) |
| 2968 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) |
| 2969 | if (II->getIntrinsicID() == IID) |
| 2970 | return II; |
| 2971 | |
| 2972 | return 0; |
| 2973 | } |
| 2974 | |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 2975 | template <typename IterTy> |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 2976 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 2977 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 2978 | Type *Ty = V->getType(); |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 2979 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 2980 | Ty = PTy->getElementType(); |
| 2981 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 2982 | |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2983 | // call undef -> undef |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 2984 | if (isa<UndefValue>(V)) |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 2985 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 2986 | |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 2987 | Function *F = dyn_cast<Function>(V); |
| 2988 | if (!F) |
| 2989 | return 0; |
| 2990 | |
Michael Ilseman | f89de81 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 2991 | if (unsigned IID = F->getIntrinsicID()) |
| 2992 | if (Value *Ret = |
| 2993 | SimplifyIntrinsic((Intrinsic::ID) IID, ArgBegin, ArgEnd, Q, MaxRecurse)) |
| 2994 | return Ret; |
| 2995 | |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 2996 | if (!canConstantFoldCallTo(F)) |
| 2997 | return 0; |
| 2998 | |
| 2999 | SmallVector<Constant *, 4> ConstantArgs; |
| 3000 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 3001 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 3002 | Constant *C = dyn_cast<Constant>(*I); |
| 3003 | if (!C) |
| 3004 | return 0; |
| 3005 | ConstantArgs.push_back(C); |
| 3006 | } |
| 3007 | |
| 3008 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3011 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3012 | User::op_iterator ArgEnd, const DataLayout *TD, |
| 3013 | const TargetLibraryInfo *TLI, |
| 3014 | const DominatorTree *DT) { |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3015 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(TD, TLI, DT), |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3016 | RecursionLimit); |
| 3017 | } |
| 3018 | |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3019 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3020 | const DataLayout *TD, const TargetLibraryInfo *TLI, |
| 3021 | const DominatorTree *DT) { |
Chandler Carruth | e949aa1 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3022 | return ::SimplifyCall(V, Args.begin(), Args.end(), Query(TD, TLI, DT), |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3023 | RecursionLimit); |
| 3024 | } |
| 3025 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3026 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 3027 | /// instruction. If not, this returns null. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3028 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout *TD, |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3029 | const TargetLibraryInfo *TLI, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 3030 | const DominatorTree *DT) { |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3031 | Value *Result; |
| 3032 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3033 | switch (I->getOpcode()) { |
| 3034 | default: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3035 | Result = ConstantFoldInstruction(I, TD, TLI); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3036 | break; |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3037 | case Instruction::FAdd: |
| 3038 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
| 3039 | I->getFastMathFlags(), TD, TLI, DT); |
| 3040 | break; |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 3041 | case Instruction::Add: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3042 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 3043 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3044 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3045 | TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3046 | break; |
Michael Ilseman | 09ee250 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3047 | case Instruction::FSub: |
| 3048 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
| 3049 | I->getFastMathFlags(), TD, TLI, DT); |
| 3050 | break; |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3051 | case Instruction::Sub: |
| 3052 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 3053 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3054 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3055 | TD, TLI, DT); |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3056 | break; |
Michael Ilseman | eb61c92 | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 3057 | case Instruction::FMul: |
| 3058 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
| 3059 | I->getFastMathFlags(), TD, TLI, DT); |
| 3060 | break; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3061 | case Instruction::Mul: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3062 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3063 | break; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3064 | case Instruction::SDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3065 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3066 | break; |
| 3067 | case Instruction::UDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3068 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3069 | break; |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3070 | case Instruction::FDiv: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3071 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3072 | break; |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3073 | case Instruction::SRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3074 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3075 | break; |
| 3076 | case Instruction::URem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3077 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3078 | break; |
| 3079 | case Instruction::FRem: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3080 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | f24ed77 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3081 | break; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3082 | case Instruction::Shl: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3083 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 3084 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3085 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3086 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3087 | break; |
| 3088 | case Instruction::LShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3089 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
| 3090 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3091 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | case Instruction::AShr: |
Chris Lattner | 81a0dc9 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3094 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
| 3095 | cast<BinaryOperator>(I)->isExact(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3096 | TD, TLI, DT); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3097 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3098 | case Instruction::And: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3099 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3100 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3101 | case Instruction::Or: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3102 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3103 | break; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3104 | case Instruction::Xor: |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3105 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3106 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3107 | case Instruction::ICmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3108 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3109 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3110 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3111 | case Instruction::FCmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3112 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3113 | I->getOperand(0), I->getOperand(1), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3114 | break; |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3115 | case Instruction::Select: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3116 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3117 | I->getOperand(2), TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3118 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3119 | case Instruction::GetElementPtr: { |
| 3120 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3121 | Result = SimplifyGEPInst(Ops, TD, TLI, DT); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3122 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3123 | } |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3124 | case Instruction::InsertValue: { |
| 3125 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 3126 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 3127 | IV->getInsertedValueOperand(), |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3128 | IV->getIndices(), TD, TLI, DT); |
Duncan Sands | dabc280 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3129 | break; |
| 3130 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 3131 | case Instruction::PHI: |
Duncan Sands | 0aa85eb | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3132 | Result = SimplifyPHINode(cast<PHINode>(I), Query (TD, TLI, DT)); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3133 | break; |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3134 | case Instruction::Call: { |
| 3135 | CallSite CS(cast<CallInst>(I)); |
| 3136 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), |
| 3137 | TD, TLI, DT); |
Dan Gohman | 71d0503 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3138 | break; |
Chandler Carruth | c98bd9f | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3139 | } |
Duncan Sands | bd0fe56 | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3140 | case Instruction::Trunc: |
| 3141 | Result = SimplifyTruncInst(I->getOperand(0), I->getType(), TD, TLI, DT); |
| 3142 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3143 | } |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3144 | |
| 3145 | /// If called on unreachable code, the above logic may report that the |
| 3146 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 3147 | /// detecting that case here, returning a safe value instead. |
| 3148 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3151 | /// \brief Implementation of recursive simplification through an instructions |
| 3152 | /// uses. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3153 | /// |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3154 | /// This is the common implementation of the recursive simplification routines. |
| 3155 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 3156 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 3157 | /// instructions to process and attempt to simplify it using |
| 3158 | /// InstructionSimplify. |
| 3159 | /// |
| 3160 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 3161 | /// in simplified value does not count toward this. |
| 3162 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3163 | const DataLayout *TD, |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3164 | const TargetLibraryInfo *TLI, |
| 3165 | const DominatorTree *DT) { |
| 3166 | bool Simplified = false; |
Chandler Carruth | 6231d5b | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3167 | SmallSetVector<Instruction *, 8> Worklist; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3168 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3169 | // If we have an explicit value to collapse to, do that round of the |
| 3170 | // simplification loop by hand initially. |
| 3171 | if (SimpleV) { |
| 3172 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE; |
| 3173 | ++UI) |
Chandler Carruth | c5b785b | 2012-03-24 22:34:23 +0000 | [diff] [blame] | 3174 | if (*UI != I) |
Chandler Carruth | 6231d5b | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3175 | Worklist.insert(cast<Instruction>(*UI)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3176 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3177 | // Replace the instruction with its simplified value. |
| 3178 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 3179 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3180 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3181 | // parent block. |
| 3182 | if (I->getParent()) |
| 3183 | I->eraseFromParent(); |
| 3184 | } else { |
Chandler Carruth | 6231d5b | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3185 | Worklist.insert(I); |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3186 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3187 | |
Chandler Carruth | 6231d5b | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3188 | // Note that we must test the size on each iteration, the worklist can grow. |
| 3189 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 3190 | I = Worklist[Idx]; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3191 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3192 | // See if this instruction simplifies. |
| 3193 | SimpleV = SimplifyInstruction(I, TD, TLI, DT); |
| 3194 | if (!SimpleV) |
| 3195 | continue; |
| 3196 | |
| 3197 | Simplified = true; |
| 3198 | |
| 3199 | // Stash away all the uses of the old instruction so we can check them for |
| 3200 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 3201 | // uses of To on the recursive step in most cases. |
| 3202 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE; |
| 3203 | ++UI) |
Chandler Carruth | 6231d5b | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3204 | Worklist.insert(cast<Instruction>(*UI)); |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Replace the instruction with its simplified value. |
| 3207 | I->replaceAllUsesWith(SimpleV); |
| 3208 | |
| 3209 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3210 | // parent block. |
| 3211 | if (I->getParent()) |
| 3212 | I->eraseFromParent(); |
| 3213 | } |
| 3214 | return Simplified; |
| 3215 | } |
| 3216 | |
| 3217 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3218 | const DataLayout *TD, |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3219 | const TargetLibraryInfo *TLI, |
| 3220 | const DominatorTree *DT) { |
| 3221 | return replaceAndRecursivelySimplifyImpl(I, 0, TD, TLI, DT); |
| 3222 | } |
| 3223 | |
| 3224 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3225 | const DataLayout *TD, |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3226 | const TargetLibraryInfo *TLI, |
| 3227 | const DominatorTree *DT) { |
| 3228 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 3229 | assert(SimpleV && "Must provide a simplified value."); |
| 3230 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TD, TLI, DT); |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3231 | } |