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