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