Chris Lattner | 084a1b5 | 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 | a021988 | 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 | ed6d6c3 | 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 | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 26 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GlobalAlias.h" |
| 31 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 32 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 33 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 35 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "instsimplify" |
| 38 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 39 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 40 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 41 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 42 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 43 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 44 | struct Query { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 45 | const DataLayout *DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 46 | const TargetLibraryInfo *TLI; |
| 47 | const DominatorTree *DT; |
| 48 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 49 | Query(const DataLayout *DL, const TargetLibraryInfo *tli, |
| 50 | const DominatorTree *dt) : DL(DL), TLI(tli), DT(dt) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 54 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 55 | unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 56 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 57 | unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 58 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 59 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 60 | static Value *SimplifyTruncInst(Value *, Type *, const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 61 | |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 62 | /// getFalse - For a boolean type, or a vector of boolean type, return false, or |
| 63 | /// a vector with every element false, as appropriate for the type. |
| 64 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 65 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 66 | "Expected i1 type or a vector of i1!"); |
| 67 | return Constant::getNullValue(Ty); |
| 68 | } |
| 69 | |
| 70 | /// getTrue - For a boolean type, or a vector of boolean type, return true, or |
| 71 | /// a vector with every element true, as appropriate for the type. |
| 72 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 73 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 74 | "Expected i1 type or a vector of i1!"); |
| 75 | return Constant::getAllOnesValue(Ty); |
| 76 | } |
| 77 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 78 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 79 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 80 | Value *RHS) { |
| 81 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 82 | if (!Cmp) |
| 83 | return false; |
| 84 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 85 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 86 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 87 | return true; |
| 88 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 89 | CRHS == LHS; |
| 90 | } |
| 91 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 92 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 93 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 94 | Instruction *I = dyn_cast<Instruction>(V); |
| 95 | if (!I) |
| 96 | // Arguments and constants dominate all instructions. |
| 97 | return true; |
| 98 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 99 | // If we are processing instructions (and/or basic blocks) that have not been |
| 100 | // fully added to a function, the parent nodes may still be null. Simply |
| 101 | // return the conservative answer in these cases. |
| 102 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 103 | return false; |
| 104 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 105 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 106 | if (DT) { |
| 107 | if (!DT->isReachableFromEntry(P->getParent())) |
| 108 | return true; |
| 109 | if (!DT->isReachableFromEntry(I->getParent())) |
| 110 | return false; |
| 111 | return DT->dominates(I, P); |
| 112 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 113 | |
| 114 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 115 | // then it obviously dominates all phi nodes. |
| 116 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 117 | !isa<InvokeInst>(I)) |
| 118 | return true; |
| 119 | |
| 120 | return false; |
| 121 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 122 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 123 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 124 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 125 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 126 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 127 | /// Returns the simplified value, or null if no simplification was performed. |
| 128 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 129 | unsigned OpcToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 130 | unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 131 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 132 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 133 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 134 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 135 | |
| 136 | // Check whether the expression has the form "(A op' B) op C". |
| 137 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 138 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 139 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 140 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 141 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 142 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 143 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 144 | // They do! Return "L op' R" if it simplifies or is already available. |
| 145 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 146 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 147 | && L == B && R == A)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 148 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 149 | return LHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 150 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 151 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 152 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 153 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 154 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 155 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | // Check whether the expression has the form "A op (B op' C)". |
| 160 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 161 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 162 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 163 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 164 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 165 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 166 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 167 | // They do! Return "L op' R" if it simplifies or is already available. |
| 168 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 169 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 170 | && L == C && R == B)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 171 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 172 | return RHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 173 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 174 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 175 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 176 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 177 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 178 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 182 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 185 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 186 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 187 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 188 | const Query &Q, unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 189 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 190 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 191 | |
| 192 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 193 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 194 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 195 | |
| 196 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 197 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 198 | |
| 199 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 200 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 201 | Value *A = Op0->getOperand(0); |
| 202 | Value *B = Op0->getOperand(1); |
| 203 | Value *C = RHS; |
| 204 | |
| 205 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 206 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 207 | // It does! Return "A op V" if it simplifies or is already available. |
| 208 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 209 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 210 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 211 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 212 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 213 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 214 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 219 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 220 | Value *A = LHS; |
| 221 | Value *B = Op1->getOperand(0); |
| 222 | Value *C = Op1->getOperand(1); |
| 223 | |
| 224 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 225 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 226 | // It does! Return "V op C" if it simplifies or is already available. |
| 227 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 228 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 229 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 230 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 231 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 232 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 233 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // The remaining transforms require commutativity as well as associativity. |
| 238 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 239 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 240 | |
| 241 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 242 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 243 | Value *A = Op0->getOperand(0); |
| 244 | Value *B = Op0->getOperand(1); |
| 245 | Value *C = RHS; |
| 246 | |
| 247 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 248 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 249 | // It does! Return "V op B" if it simplifies or is already available. |
| 250 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 251 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 252 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 253 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 254 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 255 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 256 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 261 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 262 | Value *A = LHS; |
| 263 | Value *B = Op1->getOperand(0); |
| 264 | Value *C = Op1->getOperand(1); |
| 265 | |
| 266 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 267 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 268 | // It does! Return "B op V" if it simplifies or is already available. |
| 269 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 270 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 271 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 272 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 273 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 274 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 275 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 279 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 282 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 283 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 284 | /// evaluating it on both branches of the select results in the same value. |
| 285 | /// Returns the common value if so, otherwise returns null. |
| 286 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 287 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 288 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 289 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 290 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 291 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 292 | SelectInst *SI; |
| 293 | if (isa<SelectInst>(LHS)) { |
| 294 | SI = cast<SelectInst>(LHS); |
| 295 | } else { |
| 296 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 297 | SI = cast<SelectInst>(RHS); |
| 298 | } |
| 299 | |
| 300 | // Evaluate the BinOp on the true and false branches of the select. |
| 301 | Value *TV; |
| 302 | Value *FV; |
| 303 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 304 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 305 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 306 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 307 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 308 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 311 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 312 | // If they both failed to simplify then return null. |
| 313 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 314 | return TV; |
| 315 | |
| 316 | // If one branch simplified to undef, return the other one. |
| 317 | if (TV && isa<UndefValue>(TV)) |
| 318 | return FV; |
| 319 | if (FV && isa<UndefValue>(FV)) |
| 320 | return TV; |
| 321 | |
| 322 | // If applying the operation did not change the true and false select values, |
| 323 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 324 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 325 | return SI; |
| 326 | |
| 327 | // If one branch simplified and the other did not, and the simplified |
| 328 | // value is equal to the unsimplified one, return the simplified value. |
| 329 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 330 | if ((FV && !TV) || (TV && !FV)) { |
| 331 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 332 | // same as the original operation. |
| 333 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 334 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 335 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 336 | // We already know that "op" is the same as for the simplified value. See |
| 337 | // if the operands match too. If so, return the simplified value. |
| 338 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 339 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 340 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 341 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 342 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 343 | return Simplified; |
| 344 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 345 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 346 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 347 | return Simplified; |
| 348 | } |
| 349 | } |
| 350 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 351 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 355 | /// try to simplify the comparison by seeing whether both branches of the select |
| 356 | /// result in the same value. Returns the common value if so, otherwise returns |
| 357 | /// null. |
| 358 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 359 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 360 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 361 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 362 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 363 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 364 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 365 | // Make sure the select is on the LHS. |
| 366 | if (!isa<SelectInst>(LHS)) { |
| 367 | std::swap(LHS, RHS); |
| 368 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 369 | } |
| 370 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 371 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 372 | Value *Cond = SI->getCondition(); |
| 373 | Value *TV = SI->getTrueValue(); |
| 374 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 375 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 376 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 377 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 378 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 379 | if (TCmp == Cond) { |
| 380 | // It not only simplified, it simplified to the select condition. Replace |
| 381 | // it with 'true'. |
| 382 | TCmp = getTrue(Cond->getType()); |
| 383 | } else if (!TCmp) { |
| 384 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 385 | // condition then we can replace it with 'true'. Otherwise give up. |
| 386 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 387 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 388 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 391 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 392 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 393 | if (FCmp == Cond) { |
| 394 | // It not only simplified, it simplified to the select condition. Replace |
| 395 | // it with 'false'. |
| 396 | FCmp = getFalse(Cond->getType()); |
| 397 | } else if (!FCmp) { |
| 398 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 399 | // condition then we can replace it with 'false'. Otherwise give up. |
| 400 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 401 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 402 | FCmp = getFalse(Cond->getType()); |
| 403 | } |
| 404 | |
| 405 | // If both sides simplified to the same value, then use it as the result of |
| 406 | // the original comparison. |
| 407 | if (TCmp == FCmp) |
| 408 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 409 | |
| 410 | // The remaining cases only make sense if the select condition has the same |
| 411 | // type as the result of the comparison, so bail out if this is not so. |
| 412 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 413 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 414 | // If the false value simplified to false, then the result of the compare |
| 415 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 416 | // value simplified to false and the true value to true, returning "Cond". |
| 417 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 418 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 419 | return V; |
| 420 | // If the true value simplified to true, then the result of the compare |
| 421 | // is equal to "Cond || FCmp". |
| 422 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 423 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 424 | return V; |
| 425 | // Finally, if the false value simplified to true and the true value to |
| 426 | // false, then the result of the compare is equal to "!Cond". |
| 427 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 428 | if (Value *V = |
| 429 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 430 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 431 | return V; |
| 432 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 433 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 436 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 437 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 438 | /// it on the incoming phi values yields the same result for every value. If so |
| 439 | /// returns the common value, otherwise returns null. |
| 440 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 441 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 442 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 443 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 444 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 445 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 446 | PHINode *PI; |
| 447 | if (isa<PHINode>(LHS)) { |
| 448 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 449 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 450 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 451 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 452 | } else { |
| 453 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 454 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 455 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 456 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 457 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 461 | Value *CommonValue = nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 462 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 463 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 464 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 465 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 466 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 467 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 468 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 469 | // If the operation failed to simplify, or simplified to a different value |
| 470 | // to previously, then give up. |
| 471 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 472 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 473 | CommonValue = V; |
| 474 | } |
| 475 | |
| 476 | return CommonValue; |
| 477 | } |
| 478 | |
| 479 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 480 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 481 | /// incoming phi values yields the same result every time. If so returns the |
| 482 | /// common result, otherwise returns null. |
| 483 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 484 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 485 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 486 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 487 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 488 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 489 | // Make sure the phi is on the LHS. |
| 490 | if (!isa<PHINode>(LHS)) { |
| 491 | std::swap(LHS, RHS); |
| 492 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 493 | } |
| 494 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 495 | PHINode *PI = cast<PHINode>(LHS); |
| 496 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 497 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 498 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 499 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 500 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 501 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 502 | Value *CommonValue = nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 503 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 504 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 505 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | f12ba1d | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 506 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 507 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 508 | // If the operation failed to simplify, or simplified to a different value |
| 509 | // to previously, then give up. |
| 510 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 511 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 512 | CommonValue = V; |
| 513 | } |
| 514 | |
| 515 | return CommonValue; |
| 516 | } |
| 517 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 518 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 519 | /// fold the result. If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 520 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 521 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 522 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 523 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 524 | Constant *Ops[] = { CLHS, CRHS }; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 525 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), Ops, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 526 | Q.DL, Q.TLI); |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 527 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 529 | // Canonicalize the constant to the RHS. |
| 530 | std::swap(Op0, Op1); |
| 531 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 532 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 533 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 534 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 535 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 536 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 537 | // X + 0 -> X |
| 538 | if (match(Op1, m_Zero())) |
| 539 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 540 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 541 | // X + (Y - X) -> Y |
| 542 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 543 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 544 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 545 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 546 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 547 | return Y; |
| 548 | |
| 549 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 550 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 551 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 552 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 553 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 554 | /// i1 add -> xor. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 555 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 556 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 557 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 558 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 559 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 560 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 561 | MaxRecurse)) |
| 562 | return V; |
| 563 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 564 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 565 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 566 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 567 | // only if B and C are equal. If B and C are equal then (since we assume |
| 568 | // that operands have already been simplified) "select(cond, B, C)" should |
| 569 | // have been simplified to the common value of B and C already. Analysing |
| 570 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 571 | // for threading over phi nodes. |
| 572 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 573 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 576 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 577 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 578 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 579 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 580 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 583 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 584 | /// |
| 585 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 586 | /// accumulates the total constant offset applied in the returned constant. It |
| 587 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 588 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 589 | /// |
| 590 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 591 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 592 | /// folding. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 593 | static Constant *stripAndComputeConstantOffsets(const DataLayout *DL, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 594 | Value *&V, |
| 595 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 596 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 597 | |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 598 | // Without DataLayout, just be conservative for now. Theoretically, more could |
| 599 | // be done in this case. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 600 | if (!DL) |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 601 | return ConstantInt::get(IntegerType::get(V->getContext(), 64), 0); |
| 602 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 603 | Type *IntPtrTy = DL->getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 604 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 605 | |
| 606 | // Even though we don't look through PHI nodes, we could be called on an |
| 607 | // instruction in an unreachable block, which may be on a cycle. |
| 608 | SmallPtrSet<Value *, 4> Visited; |
| 609 | Visited.insert(V); |
| 610 | do { |
| 611 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 612 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 613 | !GEP->accumulateConstantOffset(*DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 614 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 615 | V = GEP->getPointerOperand(); |
| 616 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 617 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 618 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 619 | if (GA->mayBeOverridden()) |
| 620 | break; |
| 621 | V = GA->getAliasee(); |
| 622 | } else { |
| 623 | break; |
| 624 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 625 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 626 | "Unexpected operand type!"); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 627 | } while (Visited.insert(V)); |
| 628 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 629 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 630 | if (V->getType()->isVectorTy()) |
| 631 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 632 | OffsetIntPtr); |
| 633 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /// \brief Compute the constant difference between two pointer values. |
| 637 | /// If the difference is not a constant, returns zero. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 638 | static Constant *computePointerDifference(const DataLayout *DL, |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 639 | Value *LHS, Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 640 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 641 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 642 | |
| 643 | // If LHS and RHS are not related via constant offsets to the same base |
| 644 | // value, there is nothing we can do here. |
| 645 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 646 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 647 | |
| 648 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 649 | // LHS - RHS |
| 650 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 651 | // = LHSOffset - RHSOffset |
| 652 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 653 | } |
| 654 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 655 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 656 | /// fold the result. If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 657 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 658 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 659 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 660 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 661 | Constant *Ops[] = { CLHS, CRHS }; |
| 662 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 663 | Ops, Q.DL, Q.TLI); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // X - undef -> undef |
| 667 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 668 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 669 | return UndefValue::get(Op0->getType()); |
| 670 | |
| 671 | // X - 0 -> X |
| 672 | if (match(Op1, m_Zero())) |
| 673 | return Op0; |
| 674 | |
| 675 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 676 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 677 | return Constant::getNullValue(Op0->getType()); |
| 678 | |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame^] | 679 | // X - (0 - Y) -> X if the second sub is NUW. |
| 680 | // If Y != 0, 0 - Y is a poison value. |
| 681 | // If Y == 0, 0 - Y simplifies to 0. |
| 682 | if (BinaryOperator::isNeg(Op1)) { |
| 683 | if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) { |
| 684 | assert(BO->getOpcode() == Instruction::Sub && |
| 685 | "Expected a subtraction operator!"); |
| 686 | if (BO->hasNoUnsignedWrap()) |
| 687 | return Op0; |
| 688 | } |
| 689 | } |
| 690 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 691 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 692 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 693 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 694 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 695 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 696 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 697 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 698 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 699 | // It does, we successfully reassociated! |
| 700 | ++NumReassoc; |
| 701 | return W; |
| 702 | } |
| 703 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 704 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 705 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 706 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 707 | // It does, we successfully reassociated! |
| 708 | ++NumReassoc; |
| 709 | return W; |
| 710 | } |
| 711 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 712 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 713 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 714 | // For example, X - (X + 1) -> -1 |
| 715 | X = Op0; |
| 716 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 717 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 718 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 719 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 720 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 721 | // It does, we successfully reassociated! |
| 722 | ++NumReassoc; |
| 723 | return W; |
| 724 | } |
| 725 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 726 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 727 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 728 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 729 | // It does, we successfully reassociated! |
| 730 | ++NumReassoc; |
| 731 | return W; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 736 | // For example, X - (X - Y) -> Y. |
| 737 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 738 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 739 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 740 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 741 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 742 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 743 | // It does, we successfully reassociated! |
| 744 | ++NumReassoc; |
| 745 | return W; |
| 746 | } |
| 747 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 748 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 749 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 750 | match(Op1, m_Trunc(m_Value(Y)))) |
| 751 | if (X->getType() == Y->getType()) |
| 752 | // See if "V === X - Y" simplifies. |
| 753 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 754 | // It does! Now see if "trunc V" simplifies. |
| 755 | if (Value *W = SimplifyTruncInst(V, Op0->getType(), Q, MaxRecurse-1)) |
| 756 | // It does, return the simplified "trunc V". |
| 757 | return W; |
| 758 | |
| 759 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 760 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 761 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 762 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 763 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 764 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 765 | // i1 sub -> xor. |
| 766 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 767 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 768 | return V; |
| 769 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 770 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 771 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 772 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 773 | // only if B and C are equal. If B and C are equal then (since we assume |
| 774 | // that operands have already been simplified) "select(cond, B, C)" should |
| 775 | // have been simplified to the common value of B and C already. Analysing |
| 776 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 777 | // for threading over phi nodes. |
| 778 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 779 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 782 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 783 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 784 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 785 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 786 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 789 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 790 | /// returns null. |
| 791 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 792 | const Query &Q, unsigned MaxRecurse) { |
| 793 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 794 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 795 | Constant *Ops[] = { CLHS, CRHS }; |
| 796 | return ConstantFoldInstOperands(Instruction::FAdd, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 797 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | // Canonicalize the constant to the RHS. |
| 801 | std::swap(Op0, Op1); |
| 802 | } |
| 803 | |
| 804 | // fadd X, -0 ==> X |
| 805 | if (match(Op1, m_NegZero())) |
| 806 | return Op0; |
| 807 | |
| 808 | // fadd X, 0 ==> X, when we know X is not -0 |
| 809 | if (match(Op1, m_Zero()) && |
| 810 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 811 | return Op0; |
| 812 | |
| 813 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 814 | // where nnan and ninf have to occur at least once somewhere in this |
| 815 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 816 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 817 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 818 | SubOp = Op1; |
| 819 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 820 | SubOp = Op0; |
| 821 | if (SubOp) { |
| 822 | Instruction *FSub = cast<Instruction>(SubOp); |
| 823 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 824 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 825 | return Constant::getNullValue(Op0->getType()); |
| 826 | } |
| 827 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 828 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 832 | /// returns null. |
| 833 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 834 | const Query &Q, unsigned MaxRecurse) { |
| 835 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 836 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 837 | Constant *Ops[] = { CLHS, CRHS }; |
| 838 | return ConstantFoldInstOperands(Instruction::FSub, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 839 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
| 843 | // fsub X, 0 ==> X |
| 844 | if (match(Op1, m_Zero())) |
| 845 | return Op0; |
| 846 | |
| 847 | // fsub X, -0 ==> X, when we know X is not -0 |
| 848 | if (match(Op1, m_NegZero()) && |
| 849 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0))) |
| 850 | return Op0; |
| 851 | |
| 852 | // fsub 0, (fsub -0.0, X) ==> X |
| 853 | Value *X; |
| 854 | if (match(Op0, m_AnyZero())) { |
| 855 | if (match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 856 | return X; |
| 857 | if (FMF.noSignedZeros() && match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 858 | return X; |
| 859 | } |
| 860 | |
| 861 | // fsub nnan ninf x, x ==> 0.0 |
| 862 | if (FMF.noNaNs() && FMF.noInfs() && Op0 == Op1) |
| 863 | return Constant::getNullValue(Op0->getType()); |
| 864 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 865 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 868 | /// Given the operands for an FMul, see if we can fold the result |
| 869 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, |
| 870 | FastMathFlags FMF, |
| 871 | const Query &Q, |
| 872 | unsigned MaxRecurse) { |
| 873 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 874 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 875 | Constant *Ops[] = { CLHS, CRHS }; |
| 876 | return ConstantFoldInstOperands(Instruction::FMul, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 877 | Ops, Q.DL, Q.TLI); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 878 | } |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 879 | |
| 880 | // Canonicalize the constant to the RHS. |
| 881 | std::swap(Op0, Op1); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 884 | // fmul X, 1.0 ==> X |
| 885 | if (match(Op1, m_FPOne())) |
| 886 | return Op0; |
| 887 | |
| 888 | // fmul nnan nsz X, 0 ==> 0 |
| 889 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 890 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 891 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 892 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 895 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 896 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 897 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 898 | unsigned MaxRecurse) { |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 899 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 900 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 901 | Constant *Ops[] = { CLHS, CRHS }; |
| 902 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 903 | Ops, Q.DL, Q.TLI); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | // Canonicalize the constant to the RHS. |
| 907 | std::swap(Op0, Op1); |
| 908 | } |
| 909 | |
| 910 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 911 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 912 | return Constant::getNullValue(Op0->getType()); |
| 913 | |
| 914 | // X * 0 -> 0 |
| 915 | if (match(Op1, m_Zero())) |
| 916 | return Op1; |
| 917 | |
| 918 | // X * 1 -> X |
| 919 | if (match(Op1, m_One())) |
| 920 | return Op0; |
| 921 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 922 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 923 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 924 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 925 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 926 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 927 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 928 | // i1 mul -> and. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 929 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 930 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 931 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 932 | |
| 933 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 934 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 935 | MaxRecurse)) |
| 936 | return V; |
| 937 | |
| 938 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 939 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 940 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 941 | return V; |
| 942 | |
| 943 | // If the operation is with the result of a select instruction, check whether |
| 944 | // operating on either branch of the select always yields the same value. |
| 945 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 946 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 947 | MaxRecurse)) |
| 948 | return V; |
| 949 | |
| 950 | // If the operation is with the result of a phi instruction, check whether |
| 951 | // operating on all incoming values of the phi always yields the same value. |
| 952 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 953 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 954 | MaxRecurse)) |
| 955 | return V; |
| 956 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 957 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 960 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 961 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 962 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 963 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query (DL, TLI, DT), RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 967 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 968 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 969 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query (DL, TLI, DT), RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 972 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, |
| 973 | FastMathFlags FMF, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 974 | const DataLayout *DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 975 | const TargetLibraryInfo *TLI, |
| 976 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 977 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query (DL, TLI, DT), RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 980 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 981 | const TargetLibraryInfo *TLI, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 982 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 983 | return ::SimplifyMulInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 986 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 987 | /// fold the result. If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 988 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 989 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 990 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 991 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 992 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 993 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 997 | bool isSigned = Opcode == Instruction::SDiv; |
| 998 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 999 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1000 | if (match(Op1, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1001 | return Op1; |
| 1002 | |
| 1003 | // undef / X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1004 | if (match(Op0, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1005 | return Constant::getNullValue(Op0->getType()); |
| 1006 | |
| 1007 | // 0 / X -> 0, we don't need to preserve faults! |
| 1008 | if (match(Op0, m_Zero())) |
| 1009 | return Op0; |
| 1010 | |
| 1011 | // X / 1 -> X |
| 1012 | if (match(Op1, m_One())) |
| 1013 | return Op0; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1014 | |
| 1015 | if (Op0->getType()->isIntegerTy(1)) |
| 1016 | // It can't be division by zero, hence it must be division by one. |
| 1017 | return Op0; |
| 1018 | |
| 1019 | // X / X -> 1 |
| 1020 | if (Op0 == Op1) |
| 1021 | return ConstantInt::get(Op0->getType(), 1); |
| 1022 | |
| 1023 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1024 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1025 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1026 | if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1027 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1028 | // If the Mul knows it does not overflow, then we are good to go. |
| 1029 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1030 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1031 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1032 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1033 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1034 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1035 | return X; |
| 1036 | } |
| 1037 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1038 | // (X rem Y) / Y -> 0 |
| 1039 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1040 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1041 | return Constant::getNullValue(Op0->getType()); |
| 1042 | |
| 1043 | // If the operation is with the result of a select instruction, check whether |
| 1044 | // operating on either branch of the select always yields the same value. |
| 1045 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1046 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1047 | return V; |
| 1048 | |
| 1049 | // If the operation is with the result of a phi instruction, check whether |
| 1050 | // operating on all incoming values of the phi always yields the same value. |
| 1051 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1052 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1053 | return V; |
| 1054 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1055 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 1059 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1060 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1061 | unsigned MaxRecurse) { |
| 1062 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1063 | return V; |
| 1064 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1065 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1068 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1069 | const TargetLibraryInfo *TLI, |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1070 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1071 | return ::SimplifySDivInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 1075 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1076 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1077 | unsigned MaxRecurse) { |
| 1078 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1079 | return V; |
| 1080 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1081 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1084 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1085 | const TargetLibraryInfo *TLI, |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1086 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1087 | return ::SimplifyUDivInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1090 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1091 | unsigned) { |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1092 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1093 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1094 | return Op0; |
| 1095 | |
| 1096 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1097 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1098 | return Op1; |
| 1099 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1100 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1103 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1104 | const TargetLibraryInfo *TLI, |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1105 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1106 | return ::SimplifyFDivInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1109 | /// SimplifyRem - Given operands for an SRem or URem, see if we can |
| 1110 | /// fold the result. If not, this returns null. |
| 1111 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1112 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1113 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1114 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1115 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1116 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1120 | // X % undef -> undef |
| 1121 | if (match(Op1, m_Undef())) |
| 1122 | return Op1; |
| 1123 | |
| 1124 | // undef % X -> 0 |
| 1125 | if (match(Op0, m_Undef())) |
| 1126 | return Constant::getNullValue(Op0->getType()); |
| 1127 | |
| 1128 | // 0 % X -> 0, we don't need to preserve faults! |
| 1129 | if (match(Op0, m_Zero())) |
| 1130 | return Op0; |
| 1131 | |
| 1132 | // X % 0 -> undef, we don't need to preserve faults! |
| 1133 | if (match(Op1, m_Zero())) |
| 1134 | return UndefValue::get(Op0->getType()); |
| 1135 | |
| 1136 | // X % 1 -> 0 |
| 1137 | if (match(Op1, m_One())) |
| 1138 | return Constant::getNullValue(Op0->getType()); |
| 1139 | |
| 1140 | if (Op0->getType()->isIntegerTy(1)) |
| 1141 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1142 | return Constant::getNullValue(Op0->getType()); |
| 1143 | |
| 1144 | // X % X -> 0 |
| 1145 | if (Op0 == Op1) |
| 1146 | return Constant::getNullValue(Op0->getType()); |
| 1147 | |
| 1148 | // If the operation is with the result of a select instruction, check whether |
| 1149 | // operating on either branch of the select always yields the same value. |
| 1150 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1151 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1152 | return V; |
| 1153 | |
| 1154 | // If the operation is with the result of a phi instruction, check whether |
| 1155 | // operating on all incoming values of the phi always yields the same value. |
| 1156 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1157 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1158 | return V; |
| 1159 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1160 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /// SimplifySRemInst - Given operands for an SRem, see if we can |
| 1164 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1165 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1166 | unsigned MaxRecurse) { |
| 1167 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1168 | return V; |
| 1169 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1170 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1173 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1174 | const TargetLibraryInfo *TLI, |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1175 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1176 | return ::SimplifySRemInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /// SimplifyURemInst - Given operands for a URem, see if we can |
| 1180 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1181 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1182 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1183 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1184 | return V; |
| 1185 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1186 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1189 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1190 | const TargetLibraryInfo *TLI, |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1191 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1192 | return ::SimplifyURemInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1195 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1196 | unsigned) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1197 | // undef % X -> undef (the undef could be a snan). |
| 1198 | if (match(Op0, m_Undef())) |
| 1199 | return Op0; |
| 1200 | |
| 1201 | // X % undef -> undef |
| 1202 | if (match(Op1, m_Undef())) |
| 1203 | return Op1; |
| 1204 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1205 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1208 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1209 | const TargetLibraryInfo *TLI, |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1210 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1211 | return ::SimplifyFRemInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1214 | /// isUndefShift - Returns true if a shift by \c Amount always yields undef. |
| 1215 | static bool isUndefShift(Value *Amount) { |
| 1216 | Constant *C = dyn_cast<Constant>(Amount); |
| 1217 | if (!C) |
| 1218 | return false; |
| 1219 | |
| 1220 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1221 | if (isa<UndefValue>(C)) |
| 1222 | return true; |
| 1223 | |
| 1224 | // Shifting by the bitwidth or more is undefined. |
| 1225 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1226 | if (CI->getValue().getLimitedValue() >= |
| 1227 | CI->getType()->getScalarSizeInBits()) |
| 1228 | return true; |
| 1229 | |
| 1230 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1231 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1232 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1233 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1234 | return false; |
| 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1241 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1242 | /// fold the result. If not, this returns null. |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1243 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1244 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1245 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 1246 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 1247 | Constant *Ops[] = { C0, C1 }; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1248 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, Q.DL, Q.TLI); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1252 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1253 | if (match(Op0, m_Zero())) |
| 1254 | return Op0; |
| 1255 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1256 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1257 | if (match(Op1, m_Zero())) |
| 1258 | return Op0; |
| 1259 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1260 | // Fold undefined shifts. |
| 1261 | if (isUndefShift(Op1)) |
| 1262 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1263 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1264 | // If the operation is with the result of a select instruction, check whether |
| 1265 | // operating on either branch of the select always yields the same value. |
| 1266 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1267 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1268 | return V; |
| 1269 | |
| 1270 | // If the operation is with the result of a phi instruction, check whether |
| 1271 | // operating on all incoming values of the phi always yields the same value. |
| 1272 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1273 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1274 | return V; |
| 1275 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1276 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 1280 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1281 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1282 | const Query &Q, unsigned MaxRecurse) { |
| 1283 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1284 | return V; |
| 1285 | |
| 1286 | // undef << X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1287 | if (match(Op0, m_Undef())) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1288 | return Constant::getNullValue(Op0->getType()); |
| 1289 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1290 | // (X >> A) << A -> X |
| 1291 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1292 | if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1293 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1294 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1297 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1298 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1299 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1300 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1301 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 1305 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1306 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1307 | const Query &Q, unsigned MaxRecurse) { |
| 1308 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1309 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1310 | |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1311 | // X >> X -> 0 |
| 1312 | if (Op0 == Op1) |
| 1313 | return Constant::getNullValue(Op0->getType()); |
| 1314 | |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1315 | // undef >>l X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1316 | if (match(Op0, m_Undef())) |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1317 | return Constant::getNullValue(Op0->getType()); |
| 1318 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1319 | // (X << A) >> A -> X |
| 1320 | Value *X; |
| 1321 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1322 | cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap()) |
| 1323 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1324 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1325 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1328 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1329 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1330 | const TargetLibraryInfo *TLI, |
| 1331 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1332 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1333 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 1337 | /// fold the result. If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1338 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1339 | const Query &Q, unsigned MaxRecurse) { |
| 1340 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1341 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1342 | |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1343 | // X >> X -> 0 |
| 1344 | if (Op0 == Op1) |
| 1345 | return Constant::getNullValue(Op0->getType()); |
| 1346 | |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1347 | // all ones >>a X -> all ones |
| 1348 | if (match(Op0, m_AllOnes())) |
| 1349 | return Op0; |
| 1350 | |
| 1351 | // undef >>a X -> all ones |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1352 | if (match(Op0, m_Undef())) |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1353 | return Constant::getAllOnesValue(Op0->getType()); |
| 1354 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1355 | // (X << A) >> A -> X |
| 1356 | Value *X; |
| 1357 | if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) && |
| 1358 | cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap()) |
| 1359 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1360 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1361 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Suyog Sarda | 1a21220 | 2014-07-17 19:07:00 +0000 | [diff] [blame] | 1362 | unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL); |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1363 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1364 | return Op0; |
| 1365 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1366 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1369 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1370 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1371 | const TargetLibraryInfo *TLI, |
| 1372 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1373 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1374 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1377 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1378 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1379 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1380 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1381 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1382 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1383 | Constant *Ops[] = { CLHS, CRHS }; |
| 1384 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1385 | Ops, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1386 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1387 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1388 | // Canonicalize the constant to the RHS. |
| 1389 | std::swap(Op0, Op1); |
| 1390 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1392 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1393 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1394 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1396 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1397 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1398 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1399 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1400 | // X & 0 = 0 |
| 1401 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1402 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1403 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1404 | // X & -1 = X |
| 1405 | if (match(Op1, m_AllOnes())) |
| 1406 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1408 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1409 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1410 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1411 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1413 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1414 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1415 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1416 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1417 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1419 | // A & (A | ?) = A |
| 1420 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1421 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1422 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1423 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1424 | // A & (-A) = A if A is a power of two or zero. |
| 1425 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1426 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Rafael Espindola | 319f74c | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 1427 | if (isKnownToBeAPowerOfTwo(Op0, /*OrZero*/true)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1428 | return Op0; |
Rafael Espindola | 319f74c | 2012-12-13 03:37:24 +0000 | [diff] [blame] | 1429 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1430 | return Op1; |
| 1431 | } |
| 1432 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1433 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1434 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1435 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1436 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1437 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1438 | // And distributes over Or. Try some generic simplifications based on this. |
| 1439 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1440 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1441 | return V; |
| 1442 | |
| 1443 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1444 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1445 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1446 | return V; |
| 1447 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1448 | // If the operation is with the result of a select instruction, check whether |
| 1449 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1450 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1451 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1452 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1453 | return V; |
| 1454 | |
| 1455 | // If the operation is with the result of a phi instruction, check whether |
| 1456 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1457 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1458 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1459 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1460 | return V; |
| 1461 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1462 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1465 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1466 | const TargetLibraryInfo *TLI, |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1467 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1468 | return ::SimplifyAndInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1471 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1472 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1473 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1474 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1475 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1476 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1477 | Constant *Ops[] = { CLHS, CRHS }; |
| 1478 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1479 | Ops, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1480 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1482 | // Canonicalize the constant to the RHS. |
| 1483 | std::swap(Op0, Op1); |
| 1484 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1486 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1487 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1488 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1490 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1491 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1492 | return Op0; |
| 1493 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1494 | // X | 0 = X |
| 1495 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1496 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1497 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1498 | // X | -1 = -1 |
| 1499 | if (match(Op1, m_AllOnes())) |
| 1500 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1502 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1503 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1504 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1505 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1507 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1508 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1509 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1510 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1511 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1513 | // A | (A & ?) = A |
| 1514 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1515 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1516 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1517 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1518 | // ~(A & ?) | A = -1 |
| 1519 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1520 | (A == Op1 || B == Op1)) |
| 1521 | return Constant::getAllOnesValue(Op1->getType()); |
| 1522 | |
| 1523 | // A | ~(A & ?) = -1 |
| 1524 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1525 | (A == Op0 || B == Op0)) |
| 1526 | return Constant::getAllOnesValue(Op0->getType()); |
| 1527 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1528 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1529 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1530 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1531 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1532 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1533 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1534 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1535 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1536 | return V; |
| 1537 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1538 | // If the operation is with the result of a select instruction, check whether |
| 1539 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1540 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1541 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1542 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1543 | return V; |
| 1544 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1545 | // (A & C)|(B & D) |
| 1546 | Value *C = nullptr, *D = nullptr; |
| 1547 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1548 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1549 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1550 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1551 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1552 | // (A & C1)|(B & C2) |
| 1553 | // If we have: ((V + N) & C1) | (V & C2) |
| 1554 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1555 | // replace with V+N. |
| 1556 | Value *V1, *V2; |
| 1557 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1558 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1559 | // Add commutes, try both ways. |
| 1560 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 1561 | return A; |
| 1562 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 1563 | return A; |
| 1564 | } |
| 1565 | // Or commutes, try both ways. |
| 1566 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1567 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1568 | // Add commutes, try both ways. |
| 1569 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 1570 | return B; |
| 1571 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 1572 | return B; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1577 | // If the operation is with the result of a phi instruction, check whether |
| 1578 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1579 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1580 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1581 | return V; |
| 1582 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1583 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1586 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1587 | const TargetLibraryInfo *TLI, |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1588 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1589 | return ::SimplifyOrInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1590 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1591 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1592 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1593 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1594 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1595 | unsigned MaxRecurse) { |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1596 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1597 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1598 | Constant *Ops[] = { CLHS, CRHS }; |
| 1599 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1600 | Ops, Q.DL, Q.TLI); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // Canonicalize the constant to the RHS. |
| 1604 | std::swap(Op0, Op1); |
| 1605 | } |
| 1606 | |
| 1607 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1608 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1609 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1610 | |
| 1611 | // A ^ 0 = A |
| 1612 | if (match(Op1, m_Zero())) |
| 1613 | return Op0; |
| 1614 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1615 | // A ^ A = 0 |
| 1616 | if (Op0 == Op1) |
| 1617 | return Constant::getNullValue(Op0->getType()); |
| 1618 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1619 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1620 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1621 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1622 | return Constant::getAllOnesValue(Op0->getType()); |
| 1623 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1624 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1625 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1626 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1627 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1628 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1629 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1630 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1631 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1632 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1633 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1634 | // have been simplified to the common value of B and C already. Analysing |
| 1635 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1636 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1637 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1638 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1641 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1642 | const TargetLibraryInfo *TLI, |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1643 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1644 | return ::SimplifyXorInst(Op0, Op1, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1647 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1648 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1649 | } |
| 1650 | |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1651 | /// ExtractEquivalentCondition - Rummage around inside V looking for something |
| 1652 | /// equivalent to the comparison "LHS Pred RHS". Return such a value if found, |
| 1653 | /// otherwise return null. Helper function for analyzing max/min idioms. |
| 1654 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1655 | Value *LHS, Value *RHS) { |
| 1656 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1657 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1658 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1659 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1660 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1661 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1662 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1663 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1664 | return Cmp; |
| 1665 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1666 | LHS == CmpRHS && RHS == CmpLHS) |
| 1667 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1668 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1671 | // A significant optimization not implemented here is assuming that alloca |
| 1672 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 1673 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 1674 | // conservative approach. |
| 1675 | // |
| 1676 | // This is inspired in part by C++11 5.10p1: |
| 1677 | // "Two pointers of the same type compare equal if and only if they are both |
| 1678 | // null, both point to the same function, or both represent the same |
| 1679 | // address." |
| 1680 | // |
| 1681 | // This is pretty permissive. |
| 1682 | // |
| 1683 | // It's also partly due to C11 6.5.9p6: |
| 1684 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 1685 | // pointers to the same object (including a pointer to an object and a |
| 1686 | // subobject at its beginning) or function, both are pointers to one past the |
| 1687 | // last element of the same array object, or one is a pointer to one past the |
| 1688 | // end of one array object and the other is a pointer to the start of a |
NAKAMURA Takumi | 065fd35 | 2013-04-08 23:05:21 +0000 | [diff] [blame] | 1689 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1690 | // object in the address space.) |
| 1691 | // |
| 1692 | // C11's version is more restrictive, however there's no reason why an argument |
| 1693 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 1694 | // equal to the beginning of a stack object in the callee. |
| 1695 | // |
| 1696 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 1697 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 1698 | // this optimization. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1699 | static Constant *computePointerICmp(const DataLayout *DL, |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1700 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1701 | CmpInst::Predicate Pred, |
| 1702 | Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1703 | // First, skip past any trivial no-ops. |
| 1704 | LHS = LHS->stripPointerCasts(); |
| 1705 | RHS = RHS->stripPointerCasts(); |
| 1706 | |
| 1707 | // A non-null pointer is not equal to a null pointer. |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 1708 | if (llvm::isKnownNonNull(LHS, TLI) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1709 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 1710 | return ConstantInt::get(GetCompareTy(LHS), |
| 1711 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1712 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1713 | // We can only fold certain predicates on pointer comparisons. |
| 1714 | switch (Pred) { |
| 1715 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1716 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Equality comaprisons are easy to fold. |
| 1719 | case CmpInst::ICMP_EQ: |
| 1720 | case CmpInst::ICMP_NE: |
| 1721 | break; |
| 1722 | |
| 1723 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 1724 | // a GEP only protects against unsigned wrapping. |
| 1725 | case CmpInst::ICMP_UGT: |
| 1726 | case CmpInst::ICMP_UGE: |
| 1727 | case CmpInst::ICMP_ULT: |
| 1728 | case CmpInst::ICMP_ULE: |
| 1729 | // However, we have to switch them to their signed variants to handle |
| 1730 | // negative indices from the base pointer. |
| 1731 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 1732 | break; |
| 1733 | } |
| 1734 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1735 | // Strip off any constant offsets so that we can reason about them. |
| 1736 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 1737 | // here and compare base addresses like AliasAnalysis does, however there are |
| 1738 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 1739 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 1740 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1741 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 1742 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1743 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1744 | // If LHS and RHS are related via constant offsets to the same base |
| 1745 | // value, we can replace it with an icmp which just compares the offsets. |
| 1746 | if (LHS == RHS) |
| 1747 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1748 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1749 | // Various optimizations for (in)equality comparisons. |
| 1750 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 1751 | // Different non-empty allocations that exist at the same time have |
| 1752 | // different addresses (if the program can tell). Global variables always |
| 1753 | // exist, so they always exist during the lifetime of each other and all |
| 1754 | // allocas. Two different allocas usually have different addresses... |
| 1755 | // |
| 1756 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 1757 | // allocas, they may have the same address. It's tempting to reduce the |
| 1758 | // scope of the problem by only looking at *static* allocas here. That would |
| 1759 | // cover the majority of allocas while significantly reducing the likelihood |
| 1760 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 1761 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 1762 | // an entry block. Also, if we have a block that's not attached to a |
| 1763 | // function, we can't tell if it's "static" under the current definition. |
| 1764 | // Theoretically, this problem could be fixed by creating a new kind of |
| 1765 | // instruction kind specifically for static allocas. Such a new instruction |
| 1766 | // could be required to be at the top of the entry block, thus preventing it |
| 1767 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 1768 | // convert regular allocas into these special allocas. It'd be nifty. |
| 1769 | // However, until then, this problem remains open. |
| 1770 | // |
| 1771 | // So, we'll assume that two non-empty allocas have different addresses |
| 1772 | // for now. |
| 1773 | // |
| 1774 | // With all that, if the offsets are within the bounds of their allocations |
| 1775 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 1776 | // allocations aren't the same, the pointers are not equal. |
| 1777 | // |
| 1778 | // Note that it's not necessary to check for LHS being a global variable |
| 1779 | // address, due to canonicalization and constant folding. |
| 1780 | if (isa<AllocaInst>(LHS) && |
| 1781 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1782 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 1783 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1784 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1785 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1786 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 1787 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 1788 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 1789 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1790 | if (!LHSOffsetValue.isNegative() && |
| 1791 | !RHSOffsetValue.isNegative() && |
| 1792 | LHSOffsetValue.ult(LHSSize) && |
| 1793 | RHSOffsetValue.ult(RHSSize)) { |
| 1794 | return ConstantInt::get(GetCompareTy(LHS), |
| 1795 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | // Repeat the above check but this time without depending on DataLayout |
| 1800 | // or being able to compute a precise size. |
| 1801 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 1802 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 1803 | LHSOffset->isNullValue() && |
| 1804 | RHSOffset->isNullValue()) |
| 1805 | return ConstantInt::get(GetCompareTy(LHS), |
| 1806 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1807 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 1808 | |
| 1809 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 1810 | // equality comparisons concerning the result. We avoid walking the whole |
| 1811 | // chain again by starting where the last calls to |
| 1812 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1813 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 1814 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 1815 | if (LHS == RHS) |
| 1816 | return ConstantExpr::getICmp(Pred, |
| 1817 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 1818 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1822 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1823 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1825 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1826 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1827 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1828 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1829 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1830 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1831 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1832 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1833 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1834 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1835 | |
| 1836 | // If we have a constant, make sure it is on the RHS. |
| 1837 | std::swap(LHS, RHS); |
| 1838 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1839 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1840 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1841 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 1842 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1843 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1844 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1845 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1846 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1847 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1848 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1849 | |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1850 | // Special case logic when the operands have i1 type. |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 1851 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1852 | switch (Pred) { |
| 1853 | default: break; |
| 1854 | case ICmpInst::ICMP_EQ: |
| 1855 | // X == 1 -> X |
| 1856 | if (match(RHS, m_One())) |
| 1857 | return LHS; |
| 1858 | break; |
| 1859 | case ICmpInst::ICMP_NE: |
| 1860 | // X != 0 -> X |
| 1861 | if (match(RHS, m_Zero())) |
| 1862 | return LHS; |
| 1863 | break; |
| 1864 | case ICmpInst::ICMP_UGT: |
| 1865 | // X >u 0 -> X |
| 1866 | if (match(RHS, m_Zero())) |
| 1867 | return LHS; |
| 1868 | break; |
| 1869 | case ICmpInst::ICMP_UGE: |
| 1870 | // X >=u 1 -> X |
| 1871 | if (match(RHS, m_One())) |
| 1872 | return LHS; |
| 1873 | break; |
| 1874 | case ICmpInst::ICMP_SLT: |
| 1875 | // X <s 0 -> X |
| 1876 | if (match(RHS, m_Zero())) |
| 1877 | return LHS; |
| 1878 | break; |
| 1879 | case ICmpInst::ICMP_SLE: |
| 1880 | // X <=s -1 -> X |
| 1881 | if (match(RHS, m_One())) |
| 1882 | return LHS; |
| 1883 | break; |
| 1884 | } |
| 1885 | } |
| 1886 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1887 | // If we are comparing with zero then try hard since this is a common case. |
| 1888 | if (match(RHS, m_Zero())) { |
| 1889 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 1890 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1891 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1892 | case ICmpInst::ICMP_ULT: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1893 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1894 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1895 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1896 | case ICmpInst::ICMP_EQ: |
| 1897 | case ICmpInst::ICMP_ULE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1898 | if (isKnownNonZero(LHS, Q.DL)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1899 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1900 | break; |
| 1901 | case ICmpInst::ICMP_NE: |
| 1902 | case ICmpInst::ICMP_UGT: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1903 | if (isKnownNonZero(LHS, Q.DL)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1904 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1905 | break; |
| 1906 | case ICmpInst::ICMP_SLT: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1907 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1908 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1909 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1910 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1911 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1912 | break; |
| 1913 | case ICmpInst::ICMP_SLE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1914 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1915 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1916 | return getTrue(ITy); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1917 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1918 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | case ICmpInst::ICMP_SGE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1921 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1922 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1923 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1924 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1925 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | case ICmpInst::ICMP_SGT: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1928 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1929 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1930 | return getFalse(ITy); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1931 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 1932 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1933 | break; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1938 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1939 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
| 1940 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue()); |
| 1941 | if (RHS_CR.isEmptySet()) |
| 1942 | return ConstantInt::getFalse(CI->getContext()); |
| 1943 | if (RHS_CR.isFullSet()) |
| 1944 | return ConstantInt::getTrue(CI->getContext()); |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 1945 | |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1946 | // Many binary operators with constant RHS have easy to compute constant |
| 1947 | // range. Use them to check whether the comparison is a tautology. |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 1948 | unsigned Width = CI->getBitWidth(); |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1949 | APInt Lower = APInt(Width, 0); |
| 1950 | APInt Upper = APInt(Width, 0); |
| 1951 | ConstantInt *CI2; |
| 1952 | if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) { |
| 1953 | // 'urem x, CI2' produces [0, CI2). |
| 1954 | Upper = CI2->getValue(); |
| 1955 | } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) { |
| 1956 | // 'srem x, CI2' produces (-|CI2|, |CI2|). |
| 1957 | Upper = CI2->getValue().abs(); |
| 1958 | Lower = (-Upper) + 1; |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 1959 | } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) { |
| 1960 | // 'udiv CI2, x' produces [0, CI2]. |
Eli Friedman | 0bae8b2 | 2011-11-08 21:08:02 +0000 | [diff] [blame] | 1961 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1962 | } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) { |
| 1963 | // 'udiv x, CI2' produces [0, UINT_MAX / CI2]. |
| 1964 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1965 | if (!CI2->isZero()) |
| 1966 | Upper = NegOne.udiv(CI2->getValue()) + 1; |
David Majnemer | ea8d5db | 2014-05-16 16:57:04 +0000 | [diff] [blame] | 1967 | } else if (match(LHS, m_SDiv(m_ConstantInt(CI2), m_Value()))) { |
David Majnemer | 651ed5e | 2014-07-04 00:23:39 +0000 | [diff] [blame] | 1968 | if (CI2->isMinSignedValue()) { |
| 1969 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 1970 | Lower = CI2->getValue(); |
| 1971 | Upper = Lower.lshr(1) + 1; |
| 1972 | } else { |
| 1973 | // 'sdiv CI2, x' produces [-|CI2|, |CI2|]. |
| 1974 | Upper = CI2->getValue().abs() + 1; |
| 1975 | Lower = (-Upper) + 1; |
| 1976 | } |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1977 | } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) { |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1978 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 1979 | APInt IntMax = APInt::getSignedMaxValue(Width); |
David Majnemer | af9180f | 2014-07-14 20:38:45 +0000 | [diff] [blame] | 1980 | APInt Val = CI2->getValue(); |
| 1981 | if (Val.isAllOnesValue()) { |
| 1982 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 1983 | // where CI2 != -1 and CI2 != 0 and CI2 != 1 |
| 1984 | Lower = IntMin + 1; |
| 1985 | Upper = IntMax + 1; |
| 1986 | } else if (Val.countLeadingZeros() < Width - 1) { |
| 1987 | // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2] |
| 1988 | // where CI2 != -1 and CI2 != 0 and CI2 != 1 |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1989 | Lower = IntMin.sdiv(Val); |
David Majnemer | af9180f | 2014-07-14 20:38:45 +0000 | [diff] [blame] | 1990 | Upper = IntMax.sdiv(Val); |
| 1991 | if (Lower.sgt(Upper)) |
| 1992 | std::swap(Lower, Upper); |
| 1993 | Upper = Upper + 1; |
David Majnemer | 5ea4fc0 | 2014-07-14 19:49:57 +0000 | [diff] [blame] | 1994 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) { |
| 1997 | // 'lshr x, CI2' produces [0, UINT_MAX >> CI2]. |
| 1998 | APInt NegOne = APInt::getAllOnesValue(Width); |
| 1999 | if (CI2->getValue().ult(Width)) |
| 2000 | Upper = NegOne.lshr(CI2->getValue()) + 1; |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 2001 | } else if (match(LHS, m_LShr(m_ConstantInt(CI2), m_Value()))) { |
| 2002 | // 'lshr CI2, x' produces [CI2 >> (Width-1), CI2]. |
| 2003 | unsigned ShiftAmount = Width - 1; |
| 2004 | if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact()) |
| 2005 | ShiftAmount = CI2->getValue().countTrailingZeros(); |
| 2006 | Lower = CI2->getValue().lshr(ShiftAmount); |
| 2007 | Upper = CI2->getValue() + 1; |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2008 | } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) { |
| 2009 | // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2]. |
| 2010 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2011 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2012 | if (CI2->getValue().ult(Width)) { |
| 2013 | Lower = IntMin.ashr(CI2->getValue()); |
| 2014 | Upper = IntMax.ashr(CI2->getValue()) + 1; |
| 2015 | } |
David Majnemer | 78910fc | 2014-05-16 17:14:03 +0000 | [diff] [blame] | 2016 | } else if (match(LHS, m_AShr(m_ConstantInt(CI2), m_Value()))) { |
| 2017 | unsigned ShiftAmount = Width - 1; |
| 2018 | if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact()) |
| 2019 | ShiftAmount = CI2->getValue().countTrailingZeros(); |
| 2020 | if (CI2->isNegative()) { |
| 2021 | // 'ashr CI2, x' produces [CI2, CI2 >> (Width-1)] |
| 2022 | Lower = CI2->getValue(); |
| 2023 | Upper = CI2->getValue().ashr(ShiftAmount) + 1; |
| 2024 | } else { |
| 2025 | // 'ashr CI2, x' produces [CI2 >> (Width-1), CI2] |
| 2026 | Lower = CI2->getValue().ashr(ShiftAmount); |
| 2027 | Upper = CI2->getValue() + 1; |
| 2028 | } |
Nick Lewycky | 3cec6f5 | 2011-03-04 07:00:57 +0000 | [diff] [blame] | 2029 | } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) { |
| 2030 | // 'or x, CI2' produces [CI2, UINT_MAX]. |
| 2031 | Lower = CI2->getValue(); |
| 2032 | } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) { |
| 2033 | // 'and x, CI2' produces [0, CI2]. |
| 2034 | Upper = CI2->getValue() + 1; |
| 2035 | } |
| 2036 | if (Lower != Upper) { |
| 2037 | ConstantRange LHS_CR = ConstantRange(Lower, Upper); |
| 2038 | if (RHS_CR.contains(LHS_CR)) |
| 2039 | return ConstantInt::getTrue(RHS->getContext()); |
| 2040 | if (RHS_CR.inverse().contains(LHS_CR)) |
| 2041 | return ConstantInt::getFalse(RHS->getContext()); |
| 2042 | } |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2045 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 2046 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 2047 | Instruction *LI = cast<CastInst>(LHS); |
| 2048 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2049 | Type *SrcTy = SrcOp->getType(); |
| 2050 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 2053 | // if the integer type is the same size as the pointer type. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2054 | if (MaxRecurse && Q.DL && isa<PtrToIntInst>(LI) && |
| 2055 | Q.DL->getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2056 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2057 | // Transfer the cast to the constant. |
| 2058 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 2059 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2060 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2061 | return V; |
| 2062 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 2063 | if (RI->getOperand(0)->getType() == SrcTy) |
| 2064 | // Compare without the cast. |
| 2065 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2066 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2067 | return V; |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | if (isa<ZExtInst>(LHS)) { |
| 2072 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 2073 | // same type. |
| 2074 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 2075 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2076 | // Compare X and Y. Note that signed predicates become unsigned. |
| 2077 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2078 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2079 | MaxRecurse-1)) |
| 2080 | return V; |
| 2081 | } |
| 2082 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 2083 | // too. If not, then try to deduce the result of the comparison. |
| 2084 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2085 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2086 | // reextended to DstTy. |
| 2087 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2088 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 2089 | |
| 2090 | // If the re-extended constant didn't change then this is effectively |
| 2091 | // also a case of comparing two zero-extended values. |
| 2092 | if (RExt == CI && MaxRecurse) |
| 2093 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2094 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2095 | return V; |
| 2096 | |
| 2097 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 2098 | // there. Use this to work out the result of the comparison. |
| 2099 | if (RExt != CI) { |
| 2100 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2101 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2102 | // LHS <u RHS. |
| 2103 | case ICmpInst::ICMP_EQ: |
| 2104 | case ICmpInst::ICMP_UGT: |
| 2105 | case ICmpInst::ICMP_UGE: |
| 2106 | return ConstantInt::getFalse(CI->getContext()); |
| 2107 | |
| 2108 | case ICmpInst::ICMP_NE: |
| 2109 | case ICmpInst::ICMP_ULT: |
| 2110 | case ICmpInst::ICMP_ULE: |
| 2111 | return ConstantInt::getTrue(CI->getContext()); |
| 2112 | |
| 2113 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 2114 | // is non-negative then LHS <s RHS. |
| 2115 | case ICmpInst::ICMP_SGT: |
| 2116 | case ICmpInst::ICMP_SGE: |
| 2117 | return CI->getValue().isNegative() ? |
| 2118 | ConstantInt::getTrue(CI->getContext()) : |
| 2119 | ConstantInt::getFalse(CI->getContext()); |
| 2120 | |
| 2121 | case ICmpInst::ICMP_SLT: |
| 2122 | case ICmpInst::ICMP_SLE: |
| 2123 | return CI->getValue().isNegative() ? |
| 2124 | ConstantInt::getFalse(CI->getContext()) : |
| 2125 | ConstantInt::getTrue(CI->getContext()); |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | if (isa<SExtInst>(LHS)) { |
| 2132 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 2133 | // same type. |
| 2134 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 2135 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2136 | // Compare X and Y. Note that the predicate does not change. |
| 2137 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2138 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2139 | return V; |
| 2140 | } |
| 2141 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 2142 | // too. If not, then try to deduce the result of the comparison. |
| 2143 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2144 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2145 | // reextended to DstTy. |
| 2146 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2147 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 2148 | |
| 2149 | // If the re-extended constant didn't change then this is effectively |
| 2150 | // also a case of comparing two sign-extended values. |
| 2151 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2152 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2153 | return V; |
| 2154 | |
| 2155 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 2156 | // bits there. Use this to work out the result of the comparison. |
| 2157 | if (RExt != CI) { |
| 2158 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2159 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2160 | case ICmpInst::ICMP_EQ: |
| 2161 | return ConstantInt::getFalse(CI->getContext()); |
| 2162 | case ICmpInst::ICMP_NE: |
| 2163 | return ConstantInt::getTrue(CI->getContext()); |
| 2164 | |
| 2165 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 2166 | // LHS >s RHS. |
| 2167 | case ICmpInst::ICMP_SGT: |
| 2168 | case ICmpInst::ICMP_SGE: |
| 2169 | return CI->getValue().isNegative() ? |
| 2170 | ConstantInt::getTrue(CI->getContext()) : |
| 2171 | ConstantInt::getFalse(CI->getContext()); |
| 2172 | case ICmpInst::ICMP_SLT: |
| 2173 | case ICmpInst::ICMP_SLE: |
| 2174 | return CI->getValue().isNegative() ? |
| 2175 | ConstantInt::getFalse(CI->getContext()) : |
| 2176 | ConstantInt::getTrue(CI->getContext()); |
| 2177 | |
| 2178 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 2179 | // LHS >u RHS. |
| 2180 | case ICmpInst::ICMP_UGT: |
| 2181 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2182 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2183 | if (MaxRecurse) |
| 2184 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 2185 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2186 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2187 | return V; |
| 2188 | break; |
| 2189 | case ICmpInst::ICMP_ULT: |
| 2190 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2191 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2192 | if (MaxRecurse) |
| 2193 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 2194 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2195 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2196 | return V; |
| 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
Nick Lewycky | c961030 | 2014-06-19 03:35:49 +0000 | [diff] [blame] | 2204 | // If a bit is known to be zero for A and known to be one for B, |
| 2205 | // then A and B cannot be equal. |
| 2206 | if (ICmpInst::isEquality(Pred)) { |
| 2207 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2208 | uint32_t BitWidth = CI->getBitWidth(); |
| 2209 | APInt LHSKnownZero(BitWidth, 0); |
| 2210 | APInt LHSKnownOne(BitWidth, 0); |
| 2211 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne); |
| 2212 | APInt RHSKnownZero(BitWidth, 0); |
| 2213 | APInt RHSKnownOne(BitWidth, 0); |
| 2214 | computeKnownBits(RHS, RHSKnownZero, RHSKnownOne); |
| 2215 | if (((LHSKnownOne & RHSKnownZero) != 0) || |
| 2216 | ((LHSKnownZero & RHSKnownOne) != 0)) |
| 2217 | return (Pred == ICmpInst::ICMP_EQ) |
| 2218 | ? ConstantInt::getFalse(CI->getContext()) |
| 2219 | : ConstantInt::getTrue(CI->getContext()); |
| 2220 | } |
| 2221 | } |
| 2222 | |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2223 | // Special logic for binary operators. |
| 2224 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2225 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2226 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2227 | // Analyze the case when either LHS or RHS is an add instruction. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2228 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2229 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2230 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2231 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2232 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 2233 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2234 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2235 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2236 | } |
| 2237 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2238 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 2239 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2240 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2241 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2242 | } |
| 2243 | |
| 2244 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2245 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2246 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2247 | Constant::getNullValue(RHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2248 | Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2249 | return V; |
| 2250 | |
| 2251 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2252 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2253 | if (Value *V = SimplifyICmpInst(Pred, |
| 2254 | Constant::getNullValue(LHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2255 | C == LHS ? D : C, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2256 | return V; |
| 2257 | |
| 2258 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2259 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 2260 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 2261 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2262 | Value *Y, *Z; |
| 2263 | if (A == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2264 | // C + B == C + D -> B == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2265 | Y = B; |
| 2266 | Z = D; |
| 2267 | } else if (A == D) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2268 | // D + B == C + D -> B == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2269 | Y = B; |
| 2270 | Z = C; |
| 2271 | } else if (B == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2272 | // A + C == C + D -> A == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2273 | Y = A; |
| 2274 | Z = D; |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2275 | } else { |
| 2276 | assert(B == D); |
| 2277 | // A + D == C + D -> A == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2278 | Y = A; |
| 2279 | Z = C; |
| 2280 | } |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2281 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2282 | return V; |
| 2283 | } |
| 2284 | } |
| 2285 | |
David Majnemer | 2d6c023 | 2014-05-14 20:16:28 +0000 | [diff] [blame] | 2286 | // 0 - (zext X) pred C |
| 2287 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2288 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2289 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2290 | if (Pred == ICmpInst::ICMP_SLT) |
| 2291 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2292 | if (Pred == ICmpInst::ICMP_SGE) |
| 2293 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2294 | if (Pred == ICmpInst::ICMP_EQ) |
| 2295 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2296 | if (Pred == ICmpInst::ICMP_NE) |
| 2297 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2298 | } |
| 2299 | if (RHSC->getValue().isNonNegative()) { |
| 2300 | if (Pred == ICmpInst::ICMP_SLE) |
| 2301 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2302 | if (Pred == ICmpInst::ICMP_SGT) |
| 2303 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2308 | // icmp pred (urem X, Y), Y |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2309 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2310 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2311 | switch (Pred) { |
| 2312 | default: |
| 2313 | break; |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2314 | case ICmpInst::ICMP_SGT: |
| 2315 | case ICmpInst::ICMP_SGE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2316 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2317 | if (!KnownNonNegative) |
| 2318 | break; |
| 2319 | // fall-through |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2320 | case ICmpInst::ICMP_EQ: |
| 2321 | case ICmpInst::ICMP_UGT: |
| 2322 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2323 | return getFalse(ITy); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2324 | case ICmpInst::ICMP_SLT: |
| 2325 | case ICmpInst::ICMP_SLE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2326 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2327 | if (!KnownNonNegative) |
| 2328 | break; |
| 2329 | // fall-through |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2330 | case ICmpInst::ICMP_NE: |
| 2331 | case ICmpInst::ICMP_ULT: |
| 2332 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2333 | return getTrue(ITy); |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2334 | } |
| 2335 | } |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2336 | |
| 2337 | // icmp pred X, (urem Y, X) |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2338 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2339 | bool KnownNonNegative, KnownNegative; |
| 2340 | switch (Pred) { |
| 2341 | default: |
| 2342 | break; |
| 2343 | case ICmpInst::ICMP_SGT: |
| 2344 | case ICmpInst::ICMP_SGE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2345 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2346 | if (!KnownNonNegative) |
| 2347 | break; |
| 2348 | // fall-through |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2349 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2350 | case ICmpInst::ICMP_UGT: |
| 2351 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2352 | return getTrue(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2353 | case ICmpInst::ICMP_SLT: |
| 2354 | case ICmpInst::ICMP_SLE: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2355 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2356 | if (!KnownNonNegative) |
| 2357 | break; |
| 2358 | // fall-through |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2359 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2360 | case ICmpInst::ICMP_ULT: |
| 2361 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2362 | return getFalse(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2363 | } |
| 2364 | } |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2365 | |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2366 | // x udiv y <=u x. |
| 2367 | if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) { |
| 2368 | // icmp pred (X /u Y), X |
| 2369 | if (Pred == ICmpInst::ICMP_UGT) |
| 2370 | return getFalse(ITy); |
| 2371 | if (Pred == ICmpInst::ICMP_ULE) |
| 2372 | return getTrue(ITy); |
| 2373 | } |
| 2374 | |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2375 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2376 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2377 | switch (LBO->getOpcode()) { |
| 2378 | default: break; |
| 2379 | case Instruction::UDiv: |
| 2380 | case Instruction::LShr: |
| 2381 | if (ICmpInst::isSigned(Pred)) |
| 2382 | break; |
| 2383 | // fall-through |
| 2384 | case Instruction::SDiv: |
| 2385 | case Instruction::AShr: |
Eli Friedman | 8a20e66 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2386 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2389 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2390 | return V; |
| 2391 | break; |
| 2392 | case Instruction::Shl: { |
Duncan Sands | 020c194 | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2393 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2394 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2395 | if (!NUW && !NSW) |
| 2396 | break; |
| 2397 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2398 | break; |
| 2399 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2400 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2401 | return V; |
| 2402 | break; |
| 2403 | } |
| 2404 | } |
| 2405 | } |
| 2406 | |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2407 | // Simplify comparisons involving max/min. |
| 2408 | Value *A, *B; |
| 2409 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2410 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2411 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2412 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2413 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2414 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2415 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2416 | // We analyze this as smax(A, B) pred A. |
| 2417 | P = Pred; |
| 2418 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2419 | (A == LHS || B == LHS)) { |
| 2420 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2421 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2422 | // We analyze this as smax(A, B) swapped-pred A. |
| 2423 | P = CmpInst::getSwappedPredicate(Pred); |
| 2424 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2425 | (A == RHS || B == RHS)) { |
| 2426 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2427 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2428 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2429 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2430 | P = CmpInst::getSwappedPredicate(Pred); |
| 2431 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2432 | (A == LHS || B == LHS)) { |
| 2433 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2434 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2435 | // We analyze this as smax(-A, -B) pred -A. |
| 2436 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2437 | P = Pred; |
| 2438 | } |
| 2439 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2440 | // Cases correspond to "max(A, B) p A". |
| 2441 | switch (P) { |
| 2442 | default: |
| 2443 | break; |
| 2444 | case CmpInst::ICMP_EQ: |
| 2445 | case CmpInst::ICMP_SLE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2446 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2447 | // in the max/min; if so, we can just return that. |
| 2448 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2449 | return V; |
| 2450 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2451 | return V; |
| 2452 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2453 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2454 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2455 | return V; |
| 2456 | break; |
| 2457 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2458 | case CmpInst::ICMP_SGT: { |
| 2459 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2460 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2461 | // tested in the max/min; if so, we can just return that. |
| 2462 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2463 | return V; |
| 2464 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2465 | return V; |
| 2466 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2467 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2468 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2469 | return V; |
| 2470 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2471 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2472 | case CmpInst::ICMP_SGE: |
| 2473 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2474 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2475 | case CmpInst::ICMP_SLT: |
| 2476 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2477 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2481 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2482 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2483 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2484 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2485 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2486 | // We analyze this as umax(A, B) pred A. |
| 2487 | P = Pred; |
| 2488 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2489 | (A == LHS || B == LHS)) { |
| 2490 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2491 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2492 | // We analyze this as umax(A, B) swapped-pred A. |
| 2493 | P = CmpInst::getSwappedPredicate(Pred); |
| 2494 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2495 | (A == RHS || B == RHS)) { |
| 2496 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2497 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2498 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2499 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2500 | P = CmpInst::getSwappedPredicate(Pred); |
| 2501 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2502 | (A == LHS || B == LHS)) { |
| 2503 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2504 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2505 | // We analyze this as umax(-A, -B) pred -A. |
| 2506 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2507 | P = Pred; |
| 2508 | } |
| 2509 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2510 | // Cases correspond to "max(A, B) p A". |
| 2511 | switch (P) { |
| 2512 | default: |
| 2513 | break; |
| 2514 | case CmpInst::ICMP_EQ: |
| 2515 | case CmpInst::ICMP_ULE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2516 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2517 | // in the max/min; if so, we can just return that. |
| 2518 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2519 | return V; |
| 2520 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2521 | return V; |
| 2522 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2523 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2524 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2525 | return V; |
| 2526 | break; |
| 2527 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2528 | case CmpInst::ICMP_UGT: { |
| 2529 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2530 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2531 | // tested in the max/min; if so, we can just return that. |
| 2532 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2533 | return V; |
| 2534 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2535 | return V; |
| 2536 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2537 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2538 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2539 | return V; |
| 2540 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2541 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2542 | case CmpInst::ICMP_UGE: |
| 2543 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2544 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2545 | case CmpInst::ICMP_ULT: |
| 2546 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2547 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2551 | // Variants on "max(x,y) >= min(x,z)". |
| 2552 | Value *C, *D; |
| 2553 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2554 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 2555 | (A == C || A == D || B == C || B == D)) { |
| 2556 | // max(x, ?) pred min(x, ?). |
| 2557 | if (Pred == CmpInst::ICMP_SGE) |
| 2558 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2559 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2560 | if (Pred == CmpInst::ICMP_SLT) |
| 2561 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2562 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2563 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2564 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 2565 | (A == C || A == D || B == C || B == D)) { |
| 2566 | // min(x, ?) pred max(x, ?). |
| 2567 | if (Pred == CmpInst::ICMP_SLE) |
| 2568 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2569 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2570 | if (Pred == CmpInst::ICMP_SGT) |
| 2571 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2572 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2573 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2574 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 2575 | (A == C || A == D || B == C || B == D)) { |
| 2576 | // max(x, ?) pred min(x, ?). |
| 2577 | if (Pred == CmpInst::ICMP_UGE) |
| 2578 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2579 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2580 | if (Pred == CmpInst::ICMP_ULT) |
| 2581 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2582 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2583 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2584 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 2585 | (A == C || A == D || B == C || B == D)) { |
| 2586 | // min(x, ?) pred max(x, ?). |
| 2587 | if (Pred == CmpInst::ICMP_ULE) |
| 2588 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2589 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2590 | if (Pred == CmpInst::ICMP_UGT) |
| 2591 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2592 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2595 | // Simplify comparisons of related pointers using a powerful, recursive |
| 2596 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 2597 | if (LHS->getType()->isPointerTy()) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2598 | if (Constant *C = computePointerICmp(Q.DL, Q.TLI, Pred, LHS, RHS)) |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2599 | return C; |
| 2600 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 2601 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 2602 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 2603 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 2604 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 2605 | (ICmpInst::isEquality(Pred) || |
| 2606 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 2607 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 2608 | // The bases are equal and the indices are constant. Build a constant |
| 2609 | // expression GEP with the same indices and a null base pointer to see |
| 2610 | // what constant folding can make out of it. |
| 2611 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 2612 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
| 2613 | Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); |
| 2614 | |
| 2615 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
| 2616 | Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS); |
| 2617 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2622 | // If the comparison is with the result of a select instruction, check whether |
| 2623 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2624 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2625 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2626 | return V; |
| 2627 | |
| 2628 | // If the comparison is with the result of a phi instruction, check whether |
| 2629 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2630 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2631 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2632 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 2633 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2634 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2637 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2638 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2639 | const TargetLibraryInfo *TLI, |
| 2640 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2641 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2642 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2645 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 2646 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2647 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2648 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2649 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 2650 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 2651 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2652 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2653 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2654 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2656 | // If we have a constant, make sure it is on the RHS. |
| 2657 | std::swap(LHS, RHS); |
| 2658 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2659 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2661 | // Fold trivial predicates. |
| 2662 | if (Pred == FCmpInst::FCMP_FALSE) |
| 2663 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2664 | if (Pred == FCmpInst::FCMP_TRUE) |
| 2665 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2666 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2667 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 2668 | return UndefValue::get(GetCompareTy(LHS)); |
| 2669 | |
| 2670 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2671 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2672 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 2673 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 2674 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 2675 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 2676 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2677 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2678 | // Handle fcmp with constant RHS |
| 2679 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2680 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 2681 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 2682 | if (CFP->getValueAPF().isNaN()) { |
| 2683 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 2684 | return ConstantInt::getFalse(CFP->getContext()); |
| 2685 | assert(FCmpInst::isUnordered(Pred) && |
| 2686 | "Comparison must be either ordered or unordered!"); |
| 2687 | // True if unordered. |
| 2688 | return ConstantInt::getTrue(CFP->getContext()); |
| 2689 | } |
Dan Gohman | 754e4a9 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 2690 | // Check whether the constant is an infinity. |
| 2691 | if (CFP->getValueAPF().isInfinity()) { |
| 2692 | if (CFP->getValueAPF().isNegative()) { |
| 2693 | switch (Pred) { |
| 2694 | case FCmpInst::FCMP_OLT: |
| 2695 | // No value is ordered and less than negative infinity. |
| 2696 | return ConstantInt::getFalse(CFP->getContext()); |
| 2697 | case FCmpInst::FCMP_UGE: |
| 2698 | // All values are unordered with or at least negative infinity. |
| 2699 | return ConstantInt::getTrue(CFP->getContext()); |
| 2700 | default: |
| 2701 | break; |
| 2702 | } |
| 2703 | } else { |
| 2704 | switch (Pred) { |
| 2705 | case FCmpInst::FCMP_OGT: |
| 2706 | // No value is ordered and greater than infinity. |
| 2707 | return ConstantInt::getFalse(CFP->getContext()); |
| 2708 | case FCmpInst::FCMP_ULE: |
| 2709 | // All values are unordered with and at most infinity. |
| 2710 | return ConstantInt::getTrue(CFP->getContext()); |
| 2711 | default: |
| 2712 | break; |
| 2713 | } |
| 2714 | } |
| 2715 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2716 | } |
| 2717 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2718 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2719 | // If the comparison is with the result of a select instruction, check whether |
| 2720 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2721 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2722 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2723 | return V; |
| 2724 | |
| 2725 | // If the comparison is with the result of a phi instruction, check whether |
| 2726 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2727 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2728 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 2729 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 2730 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2731 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2734 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2735 | const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2736 | const TargetLibraryInfo *TLI, |
| 2737 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2738 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2739 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2742 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 2743 | /// the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2744 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 2745 | Value *FalseVal, const Query &Q, |
| 2746 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2747 | // select true, X, Y -> X |
| 2748 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 2749 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 2750 | if (CB->isAllOnesValue()) |
| 2751 | return TrueVal; |
| 2752 | if (CB->isNullValue()) |
| 2753 | return FalseVal; |
| 2754 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2756 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2757 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2758 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2760 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 2761 | if (isa<Constant>(TrueVal)) |
| 2762 | return TrueVal; |
| 2763 | return FalseVal; |
| 2764 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 2765 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 2766 | return FalseVal; |
| 2767 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 2768 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2769 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2770 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2773 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2774 | const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2775 | const TargetLibraryInfo *TLI, |
| 2776 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2777 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2778 | RecursionLimit); |
| 2779 | } |
| 2780 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2781 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 2782 | /// fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2783 | static Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2784 | // The type of the GEP pointer operand. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 2785 | PointerType *PtrTy = cast<PointerType>(Ops[0]->getType()->getScalarType()); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2787 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2788 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2789 | return Ops[0]; |
| 2790 | |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2791 | if (isa<UndefValue>(Ops[0])) { |
| 2792 | // Compute the (pointer) type returned by the GEP instruction. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2793 | Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2794 | Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 2795 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 2796 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 2797 | return UndefValue::get(GEPTy); |
| 2798 | } |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2799 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2800 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2801 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 2802 | if (match(Ops[1], m_Zero())) |
| 2803 | return Ops[0]; |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2804 | // getelementptr P, N -> P if P points to a type of zero size. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2805 | if (Q.DL) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2806 | Type *Ty = PtrTy->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2807 | if (Ty->isSized() && Q.DL->getTypeAllocSize(Ty) == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 2808 | return Ops[0]; |
| 2809 | } |
| 2810 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2811 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2812 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 2813 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2814 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2815 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2816 | |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2817 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2820 | Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2821 | const TargetLibraryInfo *TLI, |
| 2822 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2823 | return ::SimplifyGEPInst(Ops, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2826 | /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we |
| 2827 | /// can fold the result. If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2828 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2829 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 2830 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2831 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 2832 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 2833 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 2834 | |
| 2835 | // insertvalue x, undef, n -> x |
| 2836 | if (match(Val, m_Undef())) |
| 2837 | return Agg; |
| 2838 | |
| 2839 | // insertvalue x, (extractvalue y, n), n |
| 2840 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 2841 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 2842 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2843 | // insertvalue undef, (extractvalue y, n), n -> y |
| 2844 | if (match(Agg, m_Undef())) |
| 2845 | return EV->getAggregateOperand(); |
| 2846 | |
| 2847 | // insertvalue y, (extractvalue y, n), n -> y |
| 2848 | if (Agg == EV->getAggregateOperand()) |
| 2849 | return Agg; |
| 2850 | } |
| 2851 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2852 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2855 | Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 2856 | ArrayRef<unsigned> Idxs, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2857 | const DataLayout *DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2858 | const TargetLibraryInfo *TLI, |
| 2859 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2860 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2861 | RecursionLimit); |
| 2862 | } |
| 2863 | |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2864 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2865 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2866 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 2867 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2868 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2869 | bool HasUndefInput = false; |
| 2870 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2871 | Value *Incoming = PN->getIncomingValue(i); |
| 2872 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 2873 | if (Incoming == PN) continue; |
| 2874 | if (isa<UndefValue>(Incoming)) { |
| 2875 | // Remember that we saw an undef value, but otherwise ignore them. |
| 2876 | HasUndefInput = true; |
| 2877 | continue; |
| 2878 | } |
| 2879 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2880 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2881 | CommonValue = Incoming; |
| 2882 | } |
| 2883 | |
| 2884 | // If CommonValue is null then all of the incoming values were either undef or |
| 2885 | // equal to the phi node itself. |
| 2886 | if (!CommonValue) |
| 2887 | return UndefValue::get(PN->getType()); |
| 2888 | |
| 2889 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 2890 | // instruction, we cannot return X as the result of the PHI node unless it |
| 2891 | // dominates the PHI block. |
| 2892 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2893 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 2894 | |
| 2895 | return CommonValue; |
| 2896 | } |
| 2897 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2898 | static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) { |
| 2899 | if (Constant *C = dyn_cast<Constant>(Op)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2900 | return ConstantFoldInstOperands(Instruction::Trunc, Ty, C, Q.DL, Q.TLI); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2901 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2902 | return nullptr; |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2905 | Value *llvm::SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *DL, |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2906 | const TargetLibraryInfo *TLI, |
| 2907 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2908 | return ::SimplifyTruncInst(Op, Ty, Query (DL, TLI, DT), RecursionLimit); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2911 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2913 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 2914 | /// fold the result. If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2915 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2916 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2917 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2918 | case Instruction::Add: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2919 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2920 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2921 | case Instruction::FAdd: |
| 2922 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 2923 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2924 | case Instruction::Sub: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2925 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2926 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2927 | case Instruction::FSub: |
| 2928 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 2929 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2930 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 2931 | case Instruction::FMul: |
| 2932 | return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2933 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 2934 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
| 2935 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, Q, MaxRecurse); |
| 2936 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 2937 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
| 2938 | case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2939 | case Instruction::Shl: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 2940 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2941 | Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2942 | case Instruction::LShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2943 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 2944 | case Instruction::AShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2945 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
| 2946 | case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 2947 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, Q, MaxRecurse); |
| 2948 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2949 | default: |
| 2950 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 2951 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 2952 | Constant *COps[] = {CLHS, CRHS}; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2953 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, Q.DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2954 | Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2955 | } |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2956 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2957 | // If the operation is associative, try some generic simplifications. |
| 2958 | if (Instruction::isAssociative(Opcode)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2959 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2960 | return V; |
| 2961 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2962 | // If the operation is with the result of a select instruction check whether |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2963 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2964 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2965 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2966 | return V; |
| 2967 | |
| 2968 | // If the operation is with the result of a phi instruction, check whether |
| 2969 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 2970 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2971 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 2972 | return V; |
| 2973 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2974 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2975 | } |
| 2976 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2977 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2978 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2979 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2980 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2981 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query (DL, TLI, DT), RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2984 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 2985 | /// fold the result. |
| 2986 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2987 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2988 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2989 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
| 2990 | return SimplifyFCmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2994 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2995 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2996 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query (DL, TLI, DT), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2997 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2998 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 2999 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3000 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 3001 | switch (ID) { |
| 3002 | default: return false; |
| 3003 | |
| 3004 | // Unary idempotent: f(f(x)) = f(x) |
| 3005 | case Intrinsic::fabs: |
| 3006 | case Intrinsic::floor: |
| 3007 | case Intrinsic::ceil: |
| 3008 | case Intrinsic::trunc: |
| 3009 | case Intrinsic::rint: |
| 3010 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 3011 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3012 | return true; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | template <typename IterTy> |
| 3017 | static Value *SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd, |
| 3018 | const Query &Q, unsigned MaxRecurse) { |
| 3019 | // Perform idempotent optimizations |
| 3020 | if (!IsIdempotent(IID)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3021 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3022 | |
| 3023 | // Unary Ops |
| 3024 | if (std::distance(ArgBegin, ArgEnd) == 1) |
| 3025 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) |
| 3026 | if (II->getIntrinsicID() == IID) |
| 3027 | return II; |
| 3028 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3029 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3032 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3033 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3034 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3035 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3036 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 3037 | Ty = PTy->getElementType(); |
| 3038 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 3039 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3040 | // call undef -> undef |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3041 | if (isa<UndefValue>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3042 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3043 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3044 | Function *F = dyn_cast<Function>(V); |
| 3045 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3046 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3047 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3048 | if (unsigned IID = F->getIntrinsicID()) |
| 3049 | if (Value *Ret = |
| 3050 | SimplifyIntrinsic((Intrinsic::ID) IID, ArgBegin, ArgEnd, Q, MaxRecurse)) |
| 3051 | return Ret; |
| 3052 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3053 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3054 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3055 | |
| 3056 | SmallVector<Constant *, 4> ConstantArgs; |
| 3057 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 3058 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 3059 | Constant *C = dyn_cast<Constant>(*I); |
| 3060 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3061 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3062 | ConstantArgs.push_back(C); |
| 3063 | } |
| 3064 | |
| 3065 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3068 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3069 | User::op_iterator ArgEnd, const DataLayout *DL, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3070 | const TargetLibraryInfo *TLI, |
| 3071 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3072 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3073 | RecursionLimit); |
| 3074 | } |
| 3075 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 3076 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3077 | const DataLayout *DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3078 | const DominatorTree *DT) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3079 | return ::SimplifyCall(V, Args.begin(), Args.end(), Query(DL, TLI, DT), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3080 | RecursionLimit); |
| 3081 | } |
| 3082 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3083 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 3084 | /// instruction. If not, this returns null. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3085 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout *DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3086 | const TargetLibraryInfo *TLI, |
Duncan Sands | b99f39b | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 3087 | const DominatorTree *DT) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3088 | Value *Result; |
| 3089 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3090 | switch (I->getOpcode()) { |
| 3091 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3092 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3093 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3094 | case Instruction::FAdd: |
| 3095 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3096 | I->getFastMathFlags(), DL, TLI, DT); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3097 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 3098 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3099 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 3100 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3101 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3102 | DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3103 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3104 | case Instruction::FSub: |
| 3105 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3106 | I->getFastMathFlags(), DL, TLI, DT); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 3107 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3108 | case Instruction::Sub: |
| 3109 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 3110 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3111 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3112 | DL, TLI, DT); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 3113 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 3114 | case Instruction::FMul: |
| 3115 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3116 | I->getFastMathFlags(), DL, TLI, DT); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 3117 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3118 | case Instruction::Mul: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3119 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 3120 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3121 | case Instruction::SDiv: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3122 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | case Instruction::UDiv: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3125 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 3126 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3127 | case Instruction::FDiv: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3128 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 3129 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3130 | case Instruction::SRem: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3131 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3132 | break; |
| 3133 | case Instruction::URem: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3134 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3135 | break; |
| 3136 | case Instruction::FRem: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3137 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 3138 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3139 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3140 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 3141 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 3142 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3143 | DL, TLI, DT); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3144 | break; |
| 3145 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3146 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
| 3147 | cast<BinaryOperator>(I)->isExact(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3148 | DL, TLI, DT); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3149 | break; |
| 3150 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3151 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
| 3152 | cast<BinaryOperator>(I)->isExact(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3153 | DL, TLI, DT); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 3154 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3155 | case Instruction::And: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3156 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3157 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3158 | case Instruction::Or: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3159 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3160 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3161 | case Instruction::Xor: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3162 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 3163 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3164 | case Instruction::ICmp: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3165 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3166 | I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3167 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3168 | case Instruction::FCmp: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3169 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3170 | I->getOperand(0), I->getOperand(1), DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3171 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3172 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3173 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3174 | I->getOperand(2), DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3175 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3176 | case Instruction::GetElementPtr: { |
| 3177 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3178 | Result = SimplifyGEPInst(Ops, DL, TLI, DT); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3179 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3180 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3181 | case Instruction::InsertValue: { |
| 3182 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 3183 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 3184 | IV->getInsertedValueOperand(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3185 | IV->getIndices(), DL, TLI, DT); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3186 | break; |
| 3187 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 3188 | case Instruction::PHI: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3189 | Result = SimplifyPHINode(cast<PHINode>(I), Query (DL, TLI, DT)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3190 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3191 | case Instruction::Call: { |
| 3192 | CallSite CS(cast<CallInst>(I)); |
| 3193 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3194 | DL, TLI, DT); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 3195 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 3196 | } |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3197 | case Instruction::Trunc: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3198 | Result = SimplifyTruncInst(I->getOperand(0), I->getType(), DL, TLI, DT); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3199 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3200 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 3201 | |
| 3202 | /// If called on unreachable code, the above logic may report that the |
| 3203 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 3204 | /// detecting that case here, returning a safe value instead. |
| 3205 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3208 | /// \brief Implementation of recursive simplification through an instructions |
| 3209 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3210 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3211 | /// This is the common implementation of the recursive simplification routines. |
| 3212 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 3213 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 3214 | /// instructions to process and attempt to simplify it using |
| 3215 | /// InstructionSimplify. |
| 3216 | /// |
| 3217 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 3218 | /// in simplified value does not count toward this. |
| 3219 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3220 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3221 | const TargetLibraryInfo *TLI, |
| 3222 | const DominatorTree *DT) { |
| 3223 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3224 | SmallSetVector<Instruction *, 8> Worklist; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3225 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3226 | // If we have an explicit value to collapse to, do that round of the |
| 3227 | // simplification loop by hand initially. |
| 3228 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3229 | for (User *U : I->users()) |
| 3230 | if (U != I) |
| 3231 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3232 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3233 | // Replace the instruction with its simplified value. |
| 3234 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 3235 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3236 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3237 | // parent block. |
| 3238 | if (I->getParent()) |
| 3239 | I->eraseFromParent(); |
| 3240 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3241 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3242 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3243 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 3244 | // Note that we must test the size on each iteration, the worklist can grow. |
| 3245 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 3246 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3247 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3248 | // See if this instruction simplifies. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3249 | SimpleV = SimplifyInstruction(I, DL, TLI, DT); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3250 | if (!SimpleV) |
| 3251 | continue; |
| 3252 | |
| 3253 | Simplified = true; |
| 3254 | |
| 3255 | // Stash away all the uses of the old instruction so we can check them for |
| 3256 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 3257 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3258 | for (User *U : I->users()) |
| 3259 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3260 | |
| 3261 | // Replace the instruction with its simplified value. |
| 3262 | I->replaceAllUsesWith(SimpleV); |
| 3263 | |
| 3264 | // Gracefully handle edge cases where the instruction is not wired into any |
| 3265 | // parent block. |
| 3266 | if (I->getParent()) |
| 3267 | I->eraseFromParent(); |
| 3268 | } |
| 3269 | return Simplified; |
| 3270 | } |
| 3271 | |
| 3272 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3273 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3274 | const TargetLibraryInfo *TLI, |
| 3275 | const DominatorTree *DT) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3276 | return replaceAndRecursivelySimplifyImpl(I, nullptr, DL, TLI, DT); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3280 | const DataLayout *DL, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 3281 | const TargetLibraryInfo *TLI, |
| 3282 | const DominatorTree *DT) { |
| 3283 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 3284 | assert(SimpleV && "Must provide a simplified value."); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3285 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, DL, TLI, DT); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 3286 | } |