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" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CaptureTracking.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 29 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/GlobalAlias.h" |
| 34 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 35 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 36 | #include "llvm/IR/ValueHandle.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 39 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 40 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "instsimplify" |
| 42 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 43 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 44 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 45 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 46 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 47 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 48 | namespace { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 49 | struct Query { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 50 | const DataLayout &DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 51 | const TargetLibraryInfo *TLI; |
| 52 | const DominatorTree *DT; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 53 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 54 | const Instruction *CxtI; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 55 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 56 | Query(const DataLayout &DL, const TargetLibraryInfo *tli, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 57 | const DominatorTree *dt, AssumptionCache *ac = nullptr, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 58 | const Instruction *cxti = nullptr) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 59 | : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 60 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 61 | } // end anonymous namespace |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 62 | |
| 63 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 64 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 65 | unsigned); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 66 | static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
| 67 | const Query &, unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 68 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 69 | unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 70 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 71 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 72 | static Value *SimplifyCastInst(unsigned, Value *, Type *, |
| 73 | const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 74 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 75 | /// For a boolean type, or a vector of boolean type, return false, or |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 76 | /// a vector with every element false, as appropriate for the type. |
| 77 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 78 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 79 | "Expected i1 type or a vector of i1!"); |
| 80 | return Constant::getNullValue(Ty); |
| 81 | } |
| 82 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 83 | /// For a boolean type, or a vector of boolean type, return true, or |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 84 | /// a vector with every element true, as appropriate for the type. |
| 85 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 86 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 87 | "Expected i1 type or a vector of i1!"); |
| 88 | return Constant::getAllOnesValue(Ty); |
| 89 | } |
| 90 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 91 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 92 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 93 | Value *RHS) { |
| 94 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 95 | if (!Cmp) |
| 96 | return false; |
| 97 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 98 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 99 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 100 | return true; |
| 101 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 102 | CRHS == LHS; |
| 103 | } |
| 104 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 105 | /// Does the given value dominate the specified phi node? |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 106 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 107 | Instruction *I = dyn_cast<Instruction>(V); |
| 108 | if (!I) |
| 109 | // Arguments and constants dominate all instructions. |
| 110 | return true; |
| 111 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 112 | // If we are processing instructions (and/or basic blocks) that have not been |
| 113 | // fully added to a function, the parent nodes may still be null. Simply |
| 114 | // return the conservative answer in these cases. |
| 115 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 116 | return false; |
| 117 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 118 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 119 | if (DT) { |
| 120 | if (!DT->isReachableFromEntry(P->getParent())) |
| 121 | return true; |
| 122 | if (!DT->isReachableFromEntry(I->getParent())) |
| 123 | return false; |
| 124 | return DT->dominates(I, P); |
| 125 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 126 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 127 | // Otherwise, if the instruction is in the entry block and is not an invoke, |
| 128 | // then it obviously dominates all phi nodes. |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 129 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 130 | !isa<InvokeInst>(I)) |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 131 | return true; |
| 132 | |
| 133 | return false; |
| 134 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 135 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 136 | /// Simplify "A op (B op' C)" by distributing op over op', turning it into |
| 137 | /// "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 138 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 139 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 140 | /// Returns the simplified value, or null if no simplification was performed. |
| 141 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 142 | unsigned OpcToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 143 | unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 144 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 145 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 146 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 147 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 148 | |
| 149 | // Check whether the expression has the form "(A op' B) op C". |
| 150 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 151 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 152 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 153 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 154 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 155 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 156 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 157 | // They do! Return "L op' R" if it simplifies or is already available. |
| 158 | // 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] | 159 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 160 | && L == B && R == A)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 161 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 162 | return LHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 163 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 164 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 165 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 166 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 167 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 168 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // Check whether the expression has the form "A op (B op' C)". |
| 173 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 174 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 175 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 176 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 177 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 178 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 179 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 180 | // They do! Return "L op' R" if it simplifies or is already available. |
| 181 | // 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] | 182 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 183 | && L == C && R == B)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 184 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 185 | return RHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 186 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 187 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 188 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 189 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 190 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 191 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 195 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 198 | /// Generic simplifications for associative binary operations. |
| 199 | /// Returns the simpler value, or null if none was found. |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 200 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 201 | const Query &Q, unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 202 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 203 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 204 | |
| 205 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 206 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 207 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 208 | |
| 209 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 210 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 211 | |
| 212 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 213 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 214 | Value *A = Op0->getOperand(0); |
| 215 | Value *B = Op0->getOperand(1); |
| 216 | Value *C = RHS; |
| 217 | |
| 218 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 219 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 220 | // It does! Return "A op V" if it simplifies or is already available. |
| 221 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 222 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 223 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 224 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 225 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 226 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 227 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 232 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 233 | Value *A = LHS; |
| 234 | Value *B = Op1->getOperand(0); |
| 235 | Value *C = Op1->getOperand(1); |
| 236 | |
| 237 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 238 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 239 | // It does! Return "V op C" if it simplifies or is already available. |
| 240 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 241 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 242 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 243 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 244 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 245 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 246 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | // The remaining transforms require commutativity as well as associativity. |
| 251 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 252 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 253 | |
| 254 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 255 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 256 | Value *A = Op0->getOperand(0); |
| 257 | Value *B = Op0->getOperand(1); |
| 258 | Value *C = RHS; |
| 259 | |
| 260 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 261 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 262 | // It does! Return "V op B" if it simplifies or is already available. |
| 263 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 264 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 265 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 266 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 267 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 268 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 269 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 274 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 275 | Value *A = LHS; |
| 276 | Value *B = Op1->getOperand(0); |
| 277 | Value *C = Op1->getOperand(1); |
| 278 | |
| 279 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 280 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 281 | // It does! Return "B op V" if it simplifies or is already available. |
| 282 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 283 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 284 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 285 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 286 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 287 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 288 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 292 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 295 | /// In the case of a binary operation with a select instruction as an operand, |
| 296 | /// try to simplify the binop by seeing whether evaluating it on both branches |
| 297 | /// of the select results in the same value. Returns the common value if so, |
| 298 | /// otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 299 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 300 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 301 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 302 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 303 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 304 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 305 | SelectInst *SI; |
| 306 | if (isa<SelectInst>(LHS)) { |
| 307 | SI = cast<SelectInst>(LHS); |
| 308 | } else { |
| 309 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 310 | SI = cast<SelectInst>(RHS); |
| 311 | } |
| 312 | |
| 313 | // Evaluate the BinOp on the true and false branches of the select. |
| 314 | Value *TV; |
| 315 | Value *FV; |
| 316 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 317 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 318 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 319 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 320 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 321 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 324 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 325 | // If they both failed to simplify then return null. |
| 326 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 327 | return TV; |
| 328 | |
| 329 | // If one branch simplified to undef, return the other one. |
| 330 | if (TV && isa<UndefValue>(TV)) |
| 331 | return FV; |
| 332 | if (FV && isa<UndefValue>(FV)) |
| 333 | return TV; |
| 334 | |
| 335 | // If applying the operation did not change the true and false select values, |
| 336 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 337 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 338 | return SI; |
| 339 | |
| 340 | // If one branch simplified and the other did not, and the simplified |
| 341 | // value is equal to the unsimplified one, return the simplified value. |
| 342 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 343 | if ((FV && !TV) || (TV && !FV)) { |
| 344 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 345 | // same as the original operation. |
| 346 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 347 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 348 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 349 | // We already know that "op" is the same as for the simplified value. See |
| 350 | // if the operands match too. If so, return the simplified value. |
| 351 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 352 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 353 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 354 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 355 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 356 | return Simplified; |
| 357 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 358 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 359 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 360 | return Simplified; |
| 361 | } |
| 362 | } |
| 363 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 364 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 367 | /// In the case of a comparison with a select instruction, try to simplify the |
| 368 | /// comparison by seeing whether both branches of the select result in the same |
| 369 | /// value. Returns the common value if so, otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 370 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 371 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 372 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 373 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 374 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 375 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 376 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 377 | // Make sure the select is on the LHS. |
| 378 | if (!isa<SelectInst>(LHS)) { |
| 379 | std::swap(LHS, RHS); |
| 380 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 381 | } |
| 382 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 383 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 384 | Value *Cond = SI->getCondition(); |
| 385 | Value *TV = SI->getTrueValue(); |
| 386 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 387 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 388 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 389 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 390 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 391 | if (TCmp == Cond) { |
| 392 | // It not only simplified, it simplified to the select condition. Replace |
| 393 | // it with 'true'. |
| 394 | TCmp = getTrue(Cond->getType()); |
| 395 | } else if (!TCmp) { |
| 396 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 397 | // condition then we can replace it with 'true'. Otherwise give up. |
| 398 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 399 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 400 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 403 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 404 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 405 | if (FCmp == Cond) { |
| 406 | // It not only simplified, it simplified to the select condition. Replace |
| 407 | // it with 'false'. |
| 408 | FCmp = getFalse(Cond->getType()); |
| 409 | } else if (!FCmp) { |
| 410 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 411 | // condition then we can replace it with 'false'. Otherwise give up. |
| 412 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
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 | FCmp = getFalse(Cond->getType()); |
| 415 | } |
| 416 | |
| 417 | // If both sides simplified to the same value, then use it as the result of |
| 418 | // the original comparison. |
| 419 | if (TCmp == FCmp) |
| 420 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 421 | |
| 422 | // The remaining cases only make sense if the select condition has the same |
| 423 | // type as the result of the comparison, so bail out if this is not so. |
| 424 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 425 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 426 | // If the false value simplified to false, then the result of the compare |
| 427 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 428 | // value simplified to false and the true value to true, returning "Cond". |
| 429 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 430 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 431 | return V; |
| 432 | // If the true value simplified to true, then the result of the compare |
| 433 | // is equal to "Cond || FCmp". |
| 434 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 435 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 436 | return V; |
| 437 | // Finally, if the false value simplified to true and the true value to |
| 438 | // false, then the result of the compare is equal to "!Cond". |
| 439 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 440 | if (Value *V = |
| 441 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 442 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 443 | return V; |
| 444 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 445 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 448 | /// In the case of a binary operation with an operand that is a PHI instruction, |
| 449 | /// try to simplify the binop by seeing whether evaluating it on the incoming |
| 450 | /// phi values yields the same result for every value. If so returns the common |
| 451 | /// value, otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 452 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 453 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 454 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 455 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 456 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 457 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 458 | PHINode *PI; |
| 459 | if (isa<PHINode>(LHS)) { |
| 460 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 461 | // 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] | 462 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 463 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 464 | } else { |
| 465 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 466 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 467 | // 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] | 468 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 469 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 473 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 474 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 475 | // 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] | 476 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 477 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 478 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 479 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 480 | // If the operation failed to simplify, or simplified to a different value |
| 481 | // to previously, then give up. |
| 482 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 483 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 484 | CommonValue = V; |
| 485 | } |
| 486 | |
| 487 | return CommonValue; |
| 488 | } |
| 489 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 490 | /// In the case of a comparison with a PHI instruction, try to simplify the |
| 491 | /// comparison by seeing whether comparing with all of the incoming phi values |
| 492 | /// yields the same result every time. If so returns the common result, |
| 493 | /// otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 494 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 495 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 496 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 497 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 498 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 499 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 500 | // Make sure the phi is on the LHS. |
| 501 | if (!isa<PHINode>(LHS)) { |
| 502 | std::swap(LHS, RHS); |
| 503 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 504 | } |
| 505 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 506 | PHINode *PI = cast<PHINode>(LHS); |
| 507 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 508 | // 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] | 509 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 510 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 511 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 512 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 513 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 514 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 515 | // 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] | 516 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 517 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 518 | // If the operation failed to simplify, or simplified to a different value |
| 519 | // to previously, then give up. |
| 520 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 521 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 522 | CommonValue = V; |
| 523 | } |
| 524 | |
| 525 | return CommonValue; |
| 526 | } |
| 527 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 528 | /// Given operands for an Add, see if we can fold the result. |
| 529 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 530 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 531 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 532 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 533 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 534 | return ConstantFoldBinaryOpOperands(Instruction::Add, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 536 | // Canonicalize the constant to the RHS. |
| 537 | std::swap(Op0, Op1); |
| 538 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 539 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 540 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 541 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 542 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 543 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 544 | // X + 0 -> X |
| 545 | if (match(Op1, m_Zero())) |
| 546 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 547 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 548 | // X + (Y - X) -> Y |
| 549 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 550 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 551 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 552 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 553 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 554 | return Y; |
| 555 | |
| 556 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 557 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 558 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 559 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 560 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 561 | /// i1 add -> xor. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 562 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 563 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 564 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 565 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 566 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 567 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 568 | MaxRecurse)) |
| 569 | return V; |
| 570 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 571 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 572 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 573 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 574 | // only if B and C are equal. If B and C are equal then (since we assume |
| 575 | // that operands have already been simplified) "select(cond, B, C)" should |
| 576 | // have been simplified to the common value of B and C already. Analysing |
| 577 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 578 | // for threading over phi nodes. |
| 579 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 580 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 583 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 584 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 585 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 586 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 587 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
| 588 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 591 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 592 | /// |
| 593 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 594 | /// accumulates the total constant offset applied in the returned constant. It |
| 595 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 596 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 597 | /// |
| 598 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 599 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 600 | /// folding. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 601 | static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 602 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 603 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 604 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 605 | Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 606 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 607 | |
| 608 | // Even though we don't look through PHI nodes, we could be called on an |
| 609 | // instruction in an unreachable block, which may be on a cycle. |
| 610 | SmallPtrSet<Value *, 4> Visited; |
| 611 | Visited.insert(V); |
| 612 | do { |
| 613 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 614 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 615 | !GEP->accumulateConstantOffset(DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 616 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 617 | V = GEP->getPointerOperand(); |
| 618 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 619 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 620 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 621 | if (GA->isInterposable()) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 622 | break; |
| 623 | V = GA->getAliasee(); |
| 624 | } else { |
Hal Finkel | 2cac58f | 2016-07-11 03:37:59 +0000 | [diff] [blame] | 625 | if (auto CS = CallSite(V)) |
| 626 | if (Value *RV = CS.getReturnedArgOperand()) { |
| 627 | V = RV; |
| 628 | continue; |
| 629 | } |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 630 | break; |
| 631 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 632 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 633 | "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 634 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 635 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 636 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 637 | if (V->getType()->isVectorTy()) |
| 638 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 639 | OffsetIntPtr); |
| 640 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /// \brief Compute the constant difference between two pointer values. |
| 644 | /// If the difference is not a constant, returns zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 645 | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
| 646 | Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 647 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 648 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 649 | |
| 650 | // If LHS and RHS are not related via constant offsets to the same base |
| 651 | // value, there is nothing we can do here. |
| 652 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 653 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 654 | |
| 655 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 656 | // LHS - RHS |
| 657 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 658 | // = LHSOffset - RHSOffset |
| 659 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 660 | } |
| 661 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 662 | /// Given operands for a Sub, see if we can fold the result. |
| 663 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 664 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 665 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 666 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 667 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 668 | return ConstantFoldBinaryOpOperands(Instruction::Sub, CLHS, CRHS, Q.DL); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 669 | |
| 670 | // X - undef -> undef |
| 671 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 672 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 673 | return UndefValue::get(Op0->getType()); |
| 674 | |
| 675 | // X - 0 -> X |
| 676 | if (match(Op1, m_Zero())) |
| 677 | return Op0; |
| 678 | |
| 679 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 680 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 681 | return Constant::getNullValue(Op0->getType()); |
| 682 | |
David Majnemer | 4efa9ff | 2014-11-22 07:15:16 +0000 | [diff] [blame] | 683 | // 0 - X -> 0 if the sub is NUW. |
| 684 | if (isNUW && match(Op0, m_Zero())) |
| 685 | return Op0; |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 686 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 687 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 688 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 689 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 690 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 691 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 692 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 693 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 694 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 695 | // It does, we successfully reassociated! |
| 696 | ++NumReassoc; |
| 697 | return W; |
| 698 | } |
| 699 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 700 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 701 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 702 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 703 | // It does, we successfully reassociated! |
| 704 | ++NumReassoc; |
| 705 | return W; |
| 706 | } |
| 707 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 708 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 709 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 710 | // For example, X - (X + 1) -> -1 |
| 711 | X = Op0; |
| 712 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 713 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 714 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 715 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 716 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 717 | // It does, we successfully reassociated! |
| 718 | ++NumReassoc; |
| 719 | return W; |
| 720 | } |
| 721 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 722 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 723 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 724 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 725 | // It does, we successfully reassociated! |
| 726 | ++NumReassoc; |
| 727 | return W; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 732 | // For example, X - (X - Y) -> Y. |
| 733 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 734 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 735 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 736 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 737 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 738 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 739 | // It does, we successfully reassociated! |
| 740 | ++NumReassoc; |
| 741 | return W; |
| 742 | } |
| 743 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 744 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 745 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 746 | match(Op1, m_Trunc(m_Value(Y)))) |
| 747 | if (X->getType() == Y->getType()) |
| 748 | // See if "V === X - Y" simplifies. |
| 749 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 750 | // It does! Now see if "trunc V" simplifies. |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 751 | if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
| 752 | Q, MaxRecurse - 1)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 753 | // It does, return the simplified "trunc V". |
| 754 | return W; |
| 755 | |
| 756 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 757 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 758 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 759 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 760 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 761 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 762 | // i1 sub -> xor. |
| 763 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 764 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 765 | return V; |
| 766 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 767 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 768 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 769 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 770 | // only if B and C are equal. If B and C are equal then (since we assume |
| 771 | // that operands have already been simplified) "select(cond, B, C)" should |
| 772 | // have been simplified to the common value of B and C already. Analysing |
| 773 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 774 | // for threading over phi nodes. |
| 775 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 776 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 779 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 780 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 781 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 782 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 783 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
| 784 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 787 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 788 | /// returns null. |
| 789 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 790 | const Query &Q, unsigned MaxRecurse) { |
| 791 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 792 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 793 | return ConstantFoldBinaryOpOperands(Instruction::FAdd, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 794 | |
| 795 | // Canonicalize the constant to the RHS. |
| 796 | std::swap(Op0, Op1); |
| 797 | } |
| 798 | |
| 799 | // fadd X, -0 ==> X |
| 800 | if (match(Op1, m_NegZero())) |
| 801 | return Op0; |
| 802 | |
| 803 | // fadd X, 0 ==> X, when we know X is not -0 |
| 804 | if (match(Op1, m_Zero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 805 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 806 | return Op0; |
| 807 | |
| 808 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 809 | // where nnan and ninf have to occur at least once somewhere in this |
| 810 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 811 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 812 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 813 | SubOp = Op1; |
| 814 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 815 | SubOp = Op0; |
| 816 | if (SubOp) { |
| 817 | Instruction *FSub = cast<Instruction>(SubOp); |
| 818 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 819 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 820 | return Constant::getNullValue(Op0->getType()); |
| 821 | } |
| 822 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 823 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 827 | /// returns null. |
| 828 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 829 | const Query &Q, unsigned MaxRecurse) { |
| 830 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 831 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 832 | return ConstantFoldBinaryOpOperands(Instruction::FSub, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // fsub X, 0 ==> X |
| 836 | if (match(Op1, m_Zero())) |
| 837 | return Op0; |
| 838 | |
| 839 | // fsub X, -0 ==> X, when we know X is not -0 |
| 840 | if (match(Op1, m_NegZero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 841 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 842 | return Op0; |
| 843 | |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 844 | // fsub -0.0, (fsub -0.0, X) ==> X |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 845 | Value *X; |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 846 | if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 847 | return X; |
| 848 | |
| 849 | // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored. |
Benjamin Kramer | 6bb1502 | 2016-02-29 12:18:25 +0000 | [diff] [blame] | 850 | if (FMF.noSignedZeros() && match(Op0, m_AnyZero()) && |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 851 | match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 852 | return X; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 853 | |
Benjamin Kramer | 228680d | 2015-06-14 21:01:20 +0000 | [diff] [blame] | 854 | // fsub nnan x, x ==> 0.0 |
| 855 | if (FMF.noNaNs() && Op0 == Op1) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 856 | return Constant::getNullValue(Op0->getType()); |
| 857 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 858 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 861 | /// Given the operands for an FMul, see if we can fold the result |
| 862 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, |
| 863 | FastMathFlags FMF, |
| 864 | const Query &Q, |
| 865 | unsigned MaxRecurse) { |
| 866 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 867 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 868 | return ConstantFoldBinaryOpOperands(Instruction::FMul, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 869 | |
| 870 | // Canonicalize the constant to the RHS. |
| 871 | std::swap(Op0, Op1); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 874 | // fmul X, 1.0 ==> X |
| 875 | if (match(Op1, m_FPOne())) |
| 876 | return Op0; |
| 877 | |
| 878 | // fmul nnan nsz X, 0 ==> 0 |
| 879 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 880 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 881 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 882 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 885 | /// Given operands for a Mul, see if we can fold the result. |
| 886 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 887 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 888 | unsigned MaxRecurse) { |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 889 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 890 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 891 | return ConstantFoldBinaryOpOperands(Instruction::Mul, CLHS, CRHS, Q.DL); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 892 | |
| 893 | // Canonicalize the constant to the RHS. |
| 894 | std::swap(Op0, Op1); |
| 895 | } |
| 896 | |
| 897 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 898 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 899 | return Constant::getNullValue(Op0->getType()); |
| 900 | |
| 901 | // X * 0 -> 0 |
| 902 | if (match(Op1, m_Zero())) |
| 903 | return Op1; |
| 904 | |
| 905 | // X * 1 -> X |
| 906 | if (match(Op1, m_One())) |
| 907 | return Op0; |
| 908 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 909 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 910 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 911 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 912 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 913 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 914 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 915 | // i1 mul -> and. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 916 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 917 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 918 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 919 | |
| 920 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 921 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 922 | MaxRecurse)) |
| 923 | return V; |
| 924 | |
| 925 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 926 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 927 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 928 | return V; |
| 929 | |
| 930 | // If the operation is with the result of a select instruction, check whether |
| 931 | // operating on either branch of the select always yields the same value. |
| 932 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 933 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 934 | MaxRecurse)) |
| 935 | return V; |
| 936 | |
| 937 | // If the operation is with the result of a phi instruction, check whether |
| 938 | // operating on all incoming values of the phi always yields the same value. |
| 939 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 940 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 941 | MaxRecurse)) |
| 942 | return V; |
| 943 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 944 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 947 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 948 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 949 | const TargetLibraryInfo *TLI, |
| 950 | const DominatorTree *DT, AssumptionCache *AC, |
| 951 | const Instruction *CxtI) { |
| 952 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 953 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 957 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 958 | const TargetLibraryInfo *TLI, |
| 959 | const DominatorTree *DT, AssumptionCache *AC, |
| 960 | const Instruction *CxtI) { |
| 961 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 962 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 965 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 966 | const DataLayout &DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 967 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 968 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 969 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 970 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 971 | RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 974 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 975 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 976 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 977 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 978 | return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 979 | RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 982 | /// Given operands for an SDiv or UDiv, see if we can fold the result. |
| 983 | /// If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 984 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 985 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 986 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 987 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 988 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 989 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 990 | bool isSigned = Opcode == Instruction::SDiv; |
| 991 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 992 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 993 | if (match(Op1, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 994 | return Op1; |
| 995 | |
David Majnemer | 71dc8fb | 2014-12-10 07:52:18 +0000 | [diff] [blame] | 996 | // X / 0 -> undef, we don't need to preserve faults! |
| 997 | if (match(Op1, m_Zero())) |
| 998 | return UndefValue::get(Op1->getType()); |
| 999 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1000 | // undef / X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1001 | if (match(Op0, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1002 | return Constant::getNullValue(Op0->getType()); |
| 1003 | |
| 1004 | // 0 / X -> 0, we don't need to preserve faults! |
| 1005 | if (match(Op0, m_Zero())) |
| 1006 | return Op0; |
| 1007 | |
| 1008 | // X / 1 -> X |
| 1009 | if (match(Op1, m_One())) |
| 1010 | return Op0; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (Op0->getType()->isIntegerTy(1)) |
| 1013 | // It can't be division by zero, hence it must be division by one. |
| 1014 | return Op0; |
| 1015 | |
| 1016 | // X / X -> 1 |
| 1017 | if (Op0 == Op1) |
| 1018 | return ConstantInt::get(Op0->getType(), 1); |
| 1019 | |
| 1020 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1021 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1022 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1023 | 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] | 1024 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1025 | // If the Mul knows it does not overflow, then we are good to go. |
| 1026 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1027 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1028 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1029 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1030 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1031 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1032 | return X; |
| 1033 | } |
| 1034 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1035 | // (X rem Y) / Y -> 0 |
| 1036 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1037 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1038 | return Constant::getNullValue(Op0->getType()); |
| 1039 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1040 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1041 | ConstantInt *C1, *C2; |
| 1042 | if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
| 1043 | match(Op1, m_ConstantInt(C2))) { |
| 1044 | bool Overflow; |
| 1045 | C1->getValue().umul_ov(C2->getValue(), Overflow); |
| 1046 | if (Overflow) |
| 1047 | return Constant::getNullValue(Op0->getType()); |
| 1048 | } |
| 1049 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1050 | // If the operation is with the result of a select instruction, check whether |
| 1051 | // operating on either branch of the select always yields the same value. |
| 1052 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1053 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1054 | return V; |
| 1055 | |
| 1056 | // If the operation is with the result of a phi instruction, check whether |
| 1057 | // operating on all incoming values of the phi always yields the same value. |
| 1058 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1059 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1060 | return V; |
| 1061 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1062 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1065 | /// Given operands for an SDiv, see if we can fold the result. |
| 1066 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1067 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1068 | unsigned MaxRecurse) { |
| 1069 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1070 | return V; |
| 1071 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1072 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1075 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1076 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1077 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1078 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1079 | return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1080 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1083 | /// Given operands for a UDiv, see if we can fold the result. |
| 1084 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1085 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1086 | unsigned MaxRecurse) { |
| 1087 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1088 | return V; |
| 1089 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1090 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1093 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1094 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1095 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1096 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1097 | return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1098 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1101 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1102 | const Query &Q, unsigned) { |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1103 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1104 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1105 | return Op0; |
| 1106 | |
| 1107 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1108 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1109 | return Op1; |
| 1110 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1111 | // 0 / X -> 0 |
| 1112 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1113 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1114 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1115 | return Op0; |
| 1116 | |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1117 | if (FMF.noNaNs()) { |
| 1118 | // X / X -> 1.0 is legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1119 | if (Op0 == Op1) |
| 1120 | return ConstantFP::get(Op0->getType(), 1.0); |
| 1121 | |
| 1122 | // -X / X -> -1.0 and |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1123 | // X / -X -> -1.0 are legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1124 | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
| 1125 | if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && |
| 1126 | BinaryOperator::getFNegArgument(Op0) == Op1) || |
| 1127 | (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && |
| 1128 | BinaryOperator::getFNegArgument(Op1) == Op0)) |
| 1129 | return ConstantFP::get(Op0->getType(), -1.0); |
| 1130 | } |
| 1131 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1132 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1135 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1136 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1137 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1138 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1139 | const Instruction *CxtI) { |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1140 | return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1141 | RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1144 | /// Given operands for an SRem or URem, see if we can fold the result. |
| 1145 | /// If not, this returns null. |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1146 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1147 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1148 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 1149 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 1150 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1151 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1152 | // X % undef -> undef |
| 1153 | if (match(Op1, m_Undef())) |
| 1154 | return Op1; |
| 1155 | |
| 1156 | // undef % X -> 0 |
| 1157 | if (match(Op0, m_Undef())) |
| 1158 | return Constant::getNullValue(Op0->getType()); |
| 1159 | |
| 1160 | // 0 % X -> 0, we don't need to preserve faults! |
| 1161 | if (match(Op0, m_Zero())) |
| 1162 | return Op0; |
| 1163 | |
| 1164 | // X % 0 -> undef, we don't need to preserve faults! |
| 1165 | if (match(Op1, m_Zero())) |
| 1166 | return UndefValue::get(Op0->getType()); |
| 1167 | |
| 1168 | // X % 1 -> 0 |
| 1169 | if (match(Op1, m_One())) |
| 1170 | return Constant::getNullValue(Op0->getType()); |
| 1171 | |
| 1172 | if (Op0->getType()->isIntegerTy(1)) |
| 1173 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1174 | return Constant::getNullValue(Op0->getType()); |
| 1175 | |
| 1176 | // X % X -> 0 |
| 1177 | if (Op0 == Op1) |
| 1178 | return Constant::getNullValue(Op0->getType()); |
| 1179 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1180 | // (X % Y) % Y -> X % Y |
| 1181 | if ((Opcode == Instruction::SRem && |
| 1182 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1183 | (Opcode == Instruction::URem && |
| 1184 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1185 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1186 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1187 | // If the operation is with the result of a select instruction, check whether |
| 1188 | // operating on either branch of the select always yields the same value. |
| 1189 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1190 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1191 | return V; |
| 1192 | |
| 1193 | // If the operation is with the result of a phi instruction, check whether |
| 1194 | // operating on all incoming values of the phi always yields the same value. |
| 1195 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1196 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1197 | return V; |
| 1198 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1199 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1202 | /// Given operands for an SRem, see if we can fold the result. |
| 1203 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1204 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1205 | unsigned MaxRecurse) { |
| 1206 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1207 | return V; |
| 1208 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1209 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1212 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1213 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1214 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1215 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1216 | return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1217 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1220 | /// Given operands for a URem, see if we can fold the result. |
| 1221 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1222 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1223 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1224 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1225 | return V; |
| 1226 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1227 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1230 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1231 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1232 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1233 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1234 | return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1235 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1238 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1239 | const Query &, unsigned) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1240 | // undef % X -> undef (the undef could be a snan). |
| 1241 | if (match(Op0, m_Undef())) |
| 1242 | return Op0; |
| 1243 | |
| 1244 | // X % undef -> undef |
| 1245 | if (match(Op1, m_Undef())) |
| 1246 | return Op1; |
| 1247 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1248 | // 0 % X -> 0 |
| 1249 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1250 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1251 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1252 | return Op0; |
| 1253 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1254 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1257 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1258 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1259 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1260 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1261 | const Instruction *CxtI) { |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1262 | return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1263 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1266 | /// Returns true if a shift by \c Amount always yields undef. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1267 | static bool isUndefShift(Value *Amount) { |
| 1268 | Constant *C = dyn_cast<Constant>(Amount); |
| 1269 | if (!C) |
| 1270 | return false; |
| 1271 | |
| 1272 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1273 | if (isa<UndefValue>(C)) |
| 1274 | return true; |
| 1275 | |
| 1276 | // Shifting by the bitwidth or more is undefined. |
| 1277 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1278 | if (CI->getValue().getLimitedValue() >= |
| 1279 | CI->getType()->getScalarSizeInBits()) |
| 1280 | return true; |
| 1281 | |
| 1282 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1283 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1284 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1285 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1286 | return false; |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1293 | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
| 1294 | /// If not, this returns null. |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1295 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1296 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1297 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 1298 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 1299 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1300 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1301 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1302 | if (match(Op0, m_Zero())) |
| 1303 | return Op0; |
| 1304 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1305 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1306 | if (match(Op1, m_Zero())) |
| 1307 | return Op0; |
| 1308 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1309 | // Fold undefined shifts. |
| 1310 | if (isUndefShift(Op1)) |
| 1311 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1312 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1313 | // If the operation is with the result of a select instruction, check whether |
| 1314 | // operating on either branch of the select always yields the same value. |
| 1315 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1316 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1317 | return V; |
| 1318 | |
| 1319 | // If the operation is with the result of a phi instruction, check whether |
| 1320 | // operating on all incoming values of the phi always yields the same value. |
| 1321 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1322 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1323 | return V; |
| 1324 | |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1325 | // If any bits in the shift amount make that value greater than or equal to |
| 1326 | // the number of bits in the type, the shift is undefined. |
| 1327 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 1328 | APInt KnownZero(BitWidth, 0); |
| 1329 | APInt KnownOne(BitWidth, 0); |
| 1330 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
| 1331 | if (KnownOne.getLimitedValue() >= BitWidth) |
| 1332 | return UndefValue::get(Op0->getType()); |
| 1333 | |
| 1334 | // If all valid bits in the shift amount are known zero, the first operand is |
| 1335 | // unchanged. |
| 1336 | unsigned NumValidShiftBits = Log2_32_Ceil(BitWidth); |
| 1337 | APInt ShiftAmountMask = APInt::getLowBitsSet(BitWidth, NumValidShiftBits); |
| 1338 | if ((KnownZero & ShiftAmountMask) == ShiftAmountMask) |
| 1339 | return Op0; |
| 1340 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1341 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1344 | /// \brief Given operands for an Shl, LShr or AShr, see if we can |
| 1345 | /// fold the result. If not, this returns null. |
| 1346 | static Value *SimplifyRightShift(unsigned Opcode, Value *Op0, Value *Op1, |
| 1347 | bool isExact, const Query &Q, |
| 1348 | unsigned MaxRecurse) { |
| 1349 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1350 | return V; |
| 1351 | |
| 1352 | // X >> X -> 0 |
| 1353 | if (Op0 == Op1) |
| 1354 | return Constant::getNullValue(Op0->getType()); |
| 1355 | |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1356 | // undef >> X -> 0 |
| 1357 | // undef >> X -> undef (if it's exact) |
| 1358 | if (match(Op0, m_Undef())) |
| 1359 | return isExact ? Op0 : Constant::getNullValue(Op0->getType()); |
| 1360 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1361 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1362 | if (isExact) { |
| 1363 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 1364 | APInt Op0KnownZero(BitWidth, 0); |
| 1365 | APInt Op0KnownOne(BitWidth, 0); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1366 | computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC, |
| 1367 | Q.CxtI, Q.DT); |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1368 | if (Op0KnownOne[0]) |
| 1369 | return Op0; |
| 1370 | } |
| 1371 | |
| 1372 | return nullptr; |
| 1373 | } |
| 1374 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1375 | /// Given operands for an Shl, see if we can fold the result. |
| 1376 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1377 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1378 | const Query &Q, unsigned MaxRecurse) { |
| 1379 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1380 | return V; |
| 1381 | |
| 1382 | // undef << X -> 0 |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1383 | // undef << X -> undef if (if it's NSW/NUW) |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1384 | if (match(Op0, m_Undef())) |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1385 | return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType()); |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1387 | // (X >> A) << A -> X |
| 1388 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1389 | 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] | 1390 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1391 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1394 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1395 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1396 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1397 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1398 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1399 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1402 | /// Given operands for an LShr, see if we can fold the result. |
| 1403 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1404 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1405 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1406 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1407 | MaxRecurse)) |
| 1408 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1409 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1410 | // (X << A) >> A -> X |
| 1411 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1412 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1413 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1414 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1415 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1418 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1419 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1420 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1421 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1422 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1423 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1424 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1427 | /// Given operands for an AShr, see if we can fold the result. |
| 1428 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1429 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1430 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1431 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1432 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1433 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1434 | |
| 1435 | // all ones >>a X -> all ones |
| 1436 | if (match(Op0, m_AllOnes())) |
| 1437 | return Op0; |
| 1438 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1439 | // (X << A) >> A -> X |
| 1440 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1441 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1442 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1443 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1444 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1445 | unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1446 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1447 | return Op0; |
| 1448 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1449 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1452 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1453 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1454 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1455 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1456 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1457 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1458 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1461 | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
| 1462 | ICmpInst *UnsignedICmp, bool IsAnd) { |
| 1463 | Value *X, *Y; |
| 1464 | |
| 1465 | ICmpInst::Predicate EqPred; |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1466 | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
| 1467 | !ICmpInst::isEquality(EqPred)) |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1468 | return nullptr; |
| 1469 | |
| 1470 | ICmpInst::Predicate UnsignedPred; |
| 1471 | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
| 1472 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1473 | ; |
| 1474 | else if (match(UnsignedICmp, |
| 1475 | m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) && |
| 1476 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1477 | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
| 1478 | else |
| 1479 | return nullptr; |
| 1480 | |
| 1481 | // X < Y && Y != 0 --> X < Y |
| 1482 | // X < Y || Y != 0 --> Y != 0 |
| 1483 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE) |
| 1484 | return IsAnd ? UnsignedICmp : ZeroICmp; |
| 1485 | |
| 1486 | // X >= Y || Y != 0 --> true |
| 1487 | // X >= Y || Y == 0 --> X >= Y |
| 1488 | if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) { |
| 1489 | if (EqPred == ICmpInst::ICMP_NE) |
| 1490 | return getTrue(UnsignedICmp->getType()); |
| 1491 | return UnsignedICmp; |
| 1492 | } |
| 1493 | |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1494 | // X < Y && Y == 0 --> false |
| 1495 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ && |
| 1496 | IsAnd) |
| 1497 | return getFalse(UnsignedICmp->getType()); |
| 1498 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1499 | return nullptr; |
| 1500 | } |
| 1501 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1502 | static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1503 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true)) |
| 1504 | return X; |
| 1505 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1506 | // Look for this pattern: (icmp V, C0) & (icmp V, C1)). |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1507 | Type *ITy = Op0->getType(); |
| 1508 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1509 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1510 | Value *V; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1511 | if (match(Op0, m_ICmp(Pred0, m_Value(V), m_APInt(C0))) && |
| 1512 | match(Op1, m_ICmp(Pred1, m_Specific(V), m_APInt(C1)))) { |
| 1513 | // Make a constant range that's the intersection of the two icmp ranges. |
| 1514 | // If the intersection is empty, we know that the result is false. |
| 1515 | auto Range0 = ConstantRange::makeAllowedICmpRegion(Pred0, *C0); |
| 1516 | auto Range1 = ConstantRange::makeAllowedICmpRegion(Pred1, *C1); |
| 1517 | if (Range0.intersectWith(Range1).isEmptySet()) |
| 1518 | return getFalse(ITy); |
| 1519 | } |
| 1520 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame^] | 1521 | // (icmp (add V, C0), C1) & (icmp V, C0) |
| 1522 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
Sanjay Patel | f8ee0e0 | 2016-06-19 17:20:27 +0000 | [diff] [blame] | 1523 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1524 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame^] | 1525 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1526 | return nullptr; |
| 1527 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1528 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame^] | 1529 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1530 | return nullptr; |
| 1531 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1532 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1533 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1534 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame^] | 1535 | const APInt Delta = *C1 - *C0; |
| 1536 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1537 | if (Delta == 2) { |
| 1538 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1539 | return getFalse(ITy); |
| 1540 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1541 | return getFalse(ITy); |
| 1542 | } |
| 1543 | if (Delta == 1) { |
| 1544 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1545 | return getFalse(ITy); |
| 1546 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1547 | return getFalse(ITy); |
| 1548 | } |
| 1549 | } |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame^] | 1550 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1551 | if (Delta == 2) |
| 1552 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1553 | return getFalse(ITy); |
| 1554 | if (Delta == 1) |
| 1555 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1556 | return getFalse(ITy); |
| 1557 | } |
| 1558 | |
| 1559 | return nullptr; |
| 1560 | } |
| 1561 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1562 | /// Given operands for an And, see if we can fold the result. |
| 1563 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1564 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1565 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1566 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1567 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1568 | return ConstantFoldBinaryOpOperands(Instruction::And, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1570 | // Canonicalize the constant to the RHS. |
| 1571 | std::swap(Op0, Op1); |
| 1572 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1574 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1575 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1576 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1578 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1579 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1580 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1581 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1582 | // X & 0 = 0 |
| 1583 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1584 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1585 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1586 | // X & -1 = X |
| 1587 | if (match(Op1, m_AllOnes())) |
| 1588 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1589 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1590 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1591 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1592 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1593 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1594 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1595 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1596 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1597 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1598 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1599 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1601 | // A & (A | ?) = A |
| 1602 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1603 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1604 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1605 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1606 | // A & (-A) = A if A is a power of two or zero. |
| 1607 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1608 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1609 | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1610 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1611 | return Op0; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1612 | if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1613 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1614 | return Op1; |
| 1615 | } |
| 1616 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1617 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1618 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1619 | if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS)) |
| 1620 | return V; |
| 1621 | if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS)) |
| 1622 | return V; |
| 1623 | } |
| 1624 | } |
| 1625 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1626 | // The compares may be hidden behind casts. Look through those and try the |
| 1627 | // same folds as above. |
| 1628 | auto *Cast0 = dyn_cast<CastInst>(Op0); |
| 1629 | auto *Cast1 = dyn_cast<CastInst>(Op1); |
| 1630 | if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() && |
| 1631 | Cast0->getSrcTy() == Cast1->getSrcTy()) { |
| 1632 | auto *Cmp0 = dyn_cast<ICmpInst>(Cast0->getOperand(0)); |
| 1633 | auto *Cmp1 = dyn_cast<ICmpInst>(Cast1->getOperand(0)); |
| 1634 | if (Cmp0 && Cmp1) { |
| 1635 | Instruction::CastOps CastOpc = Cast0->getOpcode(); |
| 1636 | Type *ResultType = Cast0->getType(); |
| 1637 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp0, Cmp1))) |
| 1638 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1639 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp1, Cmp0))) |
| 1640 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1641 | } |
| 1642 | } |
| 1643 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1644 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1645 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1646 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1647 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1648 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1649 | // And distributes over Or. Try some generic simplifications based on this. |
| 1650 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1651 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1652 | return V; |
| 1653 | |
| 1654 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1655 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1656 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1657 | return V; |
| 1658 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1659 | // If the operation is with the result of a select instruction, check whether |
| 1660 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1661 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1662 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1663 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1664 | return V; |
| 1665 | |
| 1666 | // If the operation is with the result of a phi instruction, check whether |
| 1667 | // 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] | 1668 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1669 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1670 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1671 | return V; |
| 1672 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1673 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1676 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1677 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1678 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1679 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1680 | return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1681 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1684 | /// Simplify (or (icmp ...) (icmp ...)) to true when we can tell that the union |
| 1685 | /// contains all possible values. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1686 | static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1687 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false)) |
| 1688 | return X; |
| 1689 | |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1690 | // FIXME: Use m_APInt to allow vector splat matches. |
| 1691 | ICmpInst::Predicate Pred0, Pred1; |
| 1692 | ConstantInt *CI1, *CI2; |
| 1693 | Value *V; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1694 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_ConstantInt(CI1)), |
| 1695 | m_ConstantInt(CI2)))) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1696 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1697 | |
| 1698 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Specific(CI1)))) |
| 1699 | return nullptr; |
| 1700 | |
| 1701 | Type *ITy = Op0->getType(); |
| 1702 | |
| 1703 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1704 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1705 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1706 | |
| 1707 | const APInt &CI1V = CI1->getValue(); |
| 1708 | const APInt &CI2V = CI2->getValue(); |
| 1709 | const APInt Delta = CI2V - CI1V; |
| 1710 | if (CI1V.isStrictlyPositive()) { |
| 1711 | if (Delta == 2) { |
| 1712 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1713 | return getTrue(ITy); |
| 1714 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1715 | return getTrue(ITy); |
| 1716 | } |
| 1717 | if (Delta == 1) { |
| 1718 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1719 | return getTrue(ITy); |
| 1720 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1721 | return getTrue(ITy); |
| 1722 | } |
| 1723 | } |
| 1724 | if (CI1V.getBoolValue() && isNUW) { |
| 1725 | if (Delta == 2) |
| 1726 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1727 | return getTrue(ITy); |
| 1728 | if (Delta == 1) |
| 1729 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1730 | return getTrue(ITy); |
| 1731 | } |
| 1732 | |
| 1733 | return nullptr; |
| 1734 | } |
| 1735 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1736 | /// Given operands for an Or, see if we can fold the result. |
| 1737 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1738 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1739 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1740 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1741 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1742 | return ConstantFoldBinaryOpOperands(Instruction::Or, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1743 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1744 | // Canonicalize the constant to the RHS. |
| 1745 | std::swap(Op0, Op1); |
| 1746 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1748 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1749 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1750 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1752 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1753 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1754 | return Op0; |
| 1755 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1756 | // X | 0 = X |
| 1757 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1758 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1759 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1760 | // X | -1 = -1 |
| 1761 | if (match(Op1, m_AllOnes())) |
| 1762 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1763 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1764 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1765 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1766 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1767 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1769 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1770 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1771 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1772 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1773 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1775 | // A | (A & ?) = A |
| 1776 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1777 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1778 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1779 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1780 | // ~(A & ?) | A = -1 |
| 1781 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1782 | (A == Op1 || B == Op1)) |
| 1783 | return Constant::getAllOnesValue(Op1->getType()); |
| 1784 | |
| 1785 | // A | ~(A & ?) = -1 |
| 1786 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1787 | (A == Op0 || B == Op0)) |
| 1788 | return Constant::getAllOnesValue(Op0->getType()); |
| 1789 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1790 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1791 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1792 | if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS)) |
| 1793 | return V; |
| 1794 | if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS)) |
| 1795 | return V; |
| 1796 | } |
| 1797 | } |
| 1798 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1799 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1800 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1801 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1802 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1803 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1804 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1805 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1806 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1807 | return V; |
| 1808 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1809 | // If the operation is with the result of a select instruction, check whether |
| 1810 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1811 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1812 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1813 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1814 | return V; |
| 1815 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1816 | // (A & C)|(B & D) |
| 1817 | Value *C = nullptr, *D = nullptr; |
| 1818 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1819 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1820 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1821 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1822 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1823 | // (A & C1)|(B & C2) |
| 1824 | // If we have: ((V + N) & C1) | (V & C2) |
| 1825 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1826 | // replace with V+N. |
| 1827 | Value *V1, *V2; |
| 1828 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1829 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1830 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1831 | if (V1 == B && |
| 1832 | MaskedValueIsZero(V2, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1833 | return A; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1834 | if (V2 == B && |
| 1835 | MaskedValueIsZero(V1, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1836 | return A; |
| 1837 | } |
| 1838 | // Or commutes, try both ways. |
| 1839 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1840 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1841 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1842 | if (V1 == A && |
| 1843 | MaskedValueIsZero(V2, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1844 | return B; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1845 | if (V2 == A && |
| 1846 | MaskedValueIsZero(V1, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1847 | return B; |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1852 | // If the operation is with the result of a phi instruction, check whether |
| 1853 | // 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] | 1854 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1855 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1856 | return V; |
| 1857 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1858 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1861 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1862 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1863 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1864 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1865 | return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1866 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1867 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1868 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1869 | /// Given operands for a Xor, see if we can fold the result. |
| 1870 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1871 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1872 | unsigned MaxRecurse) { |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1873 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1874 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1875 | return ConstantFoldBinaryOpOperands(Instruction::Xor, CLHS, CRHS, Q.DL); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Canonicalize the constant to the RHS. |
| 1878 | std::swap(Op0, Op1); |
| 1879 | } |
| 1880 | |
| 1881 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1882 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1883 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1884 | |
| 1885 | // A ^ 0 = A |
| 1886 | if (match(Op1, m_Zero())) |
| 1887 | return Op0; |
| 1888 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1889 | // A ^ A = 0 |
| 1890 | if (Op0 == Op1) |
| 1891 | return Constant::getNullValue(Op0->getType()); |
| 1892 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1893 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1894 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1895 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1896 | return Constant::getAllOnesValue(Op0->getType()); |
| 1897 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1898 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1899 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1900 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1901 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1902 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1903 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1904 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1905 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1906 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1907 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1908 | // have been simplified to the common value of B and C already. Analysing |
| 1909 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1910 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1911 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1912 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1915 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1916 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1917 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1918 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1919 | return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1920 | RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1923 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1924 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1925 | } |
| 1926 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1927 | /// Rummage around inside V looking for something equivalent to the comparison |
| 1928 | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
| 1929 | /// Helper function for analyzing max/min idioms. |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1930 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 1931 | Value *LHS, Value *RHS) { |
| 1932 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 1933 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1934 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1935 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1936 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1937 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1938 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 1939 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 1940 | return Cmp; |
| 1941 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 1942 | LHS == CmpRHS && RHS == CmpLHS) |
| 1943 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1944 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1947 | // A significant optimization not implemented here is assuming that alloca |
| 1948 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 1949 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 1950 | // conservative approach. |
| 1951 | // |
| 1952 | // This is inspired in part by C++11 5.10p1: |
| 1953 | // "Two pointers of the same type compare equal if and only if they are both |
| 1954 | // null, both point to the same function, or both represent the same |
| 1955 | // address." |
| 1956 | // |
| 1957 | // This is pretty permissive. |
| 1958 | // |
| 1959 | // It's also partly due to C11 6.5.9p6: |
| 1960 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 1961 | // pointers to the same object (including a pointer to an object and a |
| 1962 | // subobject at its beginning) or function, both are pointers to one past the |
| 1963 | // last element of the same array object, or one is a pointer to one past the |
| 1964 | // 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] | 1965 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 1966 | // object in the address space.) |
| 1967 | // |
| 1968 | // C11's version is more restrictive, however there's no reason why an argument |
| 1969 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 1970 | // equal to the beginning of a stack object in the callee. |
| 1971 | // |
| 1972 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 1973 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 1974 | // this optimization. |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 1975 | static Constant * |
| 1976 | computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 1977 | const DominatorTree *DT, CmpInst::Predicate Pred, |
| 1978 | const Instruction *CxtI, Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1979 | // First, skip past any trivial no-ops. |
| 1980 | LHS = LHS->stripPointerCasts(); |
| 1981 | RHS = RHS->stripPointerCasts(); |
| 1982 | |
| 1983 | // A non-null pointer is not equal to a null pointer. |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 1984 | if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 1985 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 1986 | return ConstantInt::get(GetCompareTy(LHS), |
| 1987 | !CmpInst::isTrueWhenEqual(Pred)); |
| 1988 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1989 | // We can only fold certain predicates on pointer comparisons. |
| 1990 | switch (Pred) { |
| 1991 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1992 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Equality comaprisons are easy to fold. |
| 1995 | case CmpInst::ICMP_EQ: |
| 1996 | case CmpInst::ICMP_NE: |
| 1997 | break; |
| 1998 | |
| 1999 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 2000 | // a GEP only protects against unsigned wrapping. |
| 2001 | case CmpInst::ICMP_UGT: |
| 2002 | case CmpInst::ICMP_UGE: |
| 2003 | case CmpInst::ICMP_ULT: |
| 2004 | case CmpInst::ICMP_ULE: |
| 2005 | // However, we have to switch them to their signed variants to handle |
| 2006 | // negative indices from the base pointer. |
| 2007 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 2008 | break; |
| 2009 | } |
| 2010 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2011 | // Strip off any constant offsets so that we can reason about them. |
| 2012 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 2013 | // here and compare base addresses like AliasAnalysis does, however there are |
| 2014 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 2015 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 2016 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2017 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 2018 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2019 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2020 | // If LHS and RHS are related via constant offsets to the same base |
| 2021 | // value, we can replace it with an icmp which just compares the offsets. |
| 2022 | if (LHS == RHS) |
| 2023 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2024 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2025 | // Various optimizations for (in)equality comparisons. |
| 2026 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 2027 | // Different non-empty allocations that exist at the same time have |
| 2028 | // different addresses (if the program can tell). Global variables always |
| 2029 | // exist, so they always exist during the lifetime of each other and all |
| 2030 | // allocas. Two different allocas usually have different addresses... |
| 2031 | // |
| 2032 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 2033 | // allocas, they may have the same address. It's tempting to reduce the |
| 2034 | // scope of the problem by only looking at *static* allocas here. That would |
| 2035 | // cover the majority of allocas while significantly reducing the likelihood |
| 2036 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 2037 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 2038 | // an entry block. Also, if we have a block that's not attached to a |
| 2039 | // function, we can't tell if it's "static" under the current definition. |
| 2040 | // Theoretically, this problem could be fixed by creating a new kind of |
| 2041 | // instruction kind specifically for static allocas. Such a new instruction |
| 2042 | // could be required to be at the top of the entry block, thus preventing it |
| 2043 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 2044 | // convert regular allocas into these special allocas. It'd be nifty. |
| 2045 | // However, until then, this problem remains open. |
| 2046 | // |
| 2047 | // So, we'll assume that two non-empty allocas have different addresses |
| 2048 | // for now. |
| 2049 | // |
| 2050 | // With all that, if the offsets are within the bounds of their allocations |
| 2051 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 2052 | // allocations aren't the same, the pointers are not equal. |
| 2053 | // |
| 2054 | // Note that it's not necessary to check for LHS being a global variable |
| 2055 | // address, due to canonicalization and constant folding. |
| 2056 | if (isa<AllocaInst>(LHS) && |
| 2057 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2058 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 2059 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2060 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2061 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2062 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 2063 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2064 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 2065 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2066 | if (!LHSOffsetValue.isNegative() && |
| 2067 | !RHSOffsetValue.isNegative() && |
| 2068 | LHSOffsetValue.ult(LHSSize) && |
| 2069 | RHSOffsetValue.ult(RHSSize)) { |
| 2070 | return ConstantInt::get(GetCompareTy(LHS), |
| 2071 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | // Repeat the above check but this time without depending on DataLayout |
| 2076 | // or being able to compute a precise size. |
| 2077 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 2078 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 2079 | LHSOffset->isNullValue() && |
| 2080 | RHSOffset->isNullValue()) |
| 2081 | return ConstantInt::get(GetCompareTy(LHS), |
| 2082 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2083 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2084 | |
| 2085 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2086 | // equality comparisons concerning the result. We avoid walking the whole |
| 2087 | // chain again by starting where the last calls to |
| 2088 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2089 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2090 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2091 | if (LHS == RHS) |
| 2092 | return ConstantExpr::getICmp(Pred, |
| 2093 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2094 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2095 | |
| 2096 | // If one side of the equality comparison must come from a noalias call |
| 2097 | // (meaning a system memory allocation function), and the other side must |
| 2098 | // come from a pointer that cannot overlap with dynamically-allocated |
| 2099 | // memory within the lifetime of the current function (allocas, byval |
| 2100 | // arguments, globals), then determine the comparison result here. |
| 2101 | SmallVector<Value *, 8> LHSUObjs, RHSUObjs; |
| 2102 | GetUnderlyingObjects(LHS, LHSUObjs, DL); |
| 2103 | GetUnderlyingObjects(RHS, RHSUObjs, DL); |
| 2104 | |
| 2105 | // Is the set of underlying objects all noalias calls? |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2106 | auto IsNAC = [](ArrayRef<Value *> Objects) { |
| 2107 | return all_of(Objects, isNoAliasCall); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2108 | }; |
| 2109 | |
| 2110 | // Is the set of underlying objects all things which must be disjoint from |
Hal Finkel | aa19baf | 2014-12-04 17:45:19 +0000 | [diff] [blame] | 2111 | // noalias calls. For allocas, we consider only static ones (dynamic |
| 2112 | // allocas might be transformed into calls to malloc not simultaneously |
| 2113 | // live with the compared-to allocation). For globals, we exclude symbols |
| 2114 | // that might be resolve lazily to symbols in another dynamically-loaded |
| 2115 | // library (and, thus, could be malloc'ed by the implementation). |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2116 | auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) { |
| 2117 | return all_of(Objects, [](Value *V) { |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2118 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 2119 | return AI->getParent() && AI->getFunction() && AI->isStaticAlloca(); |
| 2120 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 2121 | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 2122 | GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2123 | !GV->isThreadLocal(); |
| 2124 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2125 | return A->hasByValAttr(); |
| 2126 | return false; |
| 2127 | }); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2128 | }; |
| 2129 | |
| 2130 | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || |
| 2131 | (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) |
| 2132 | return ConstantInt::get(GetCompareTy(LHS), |
| 2133 | !CmpInst::isTrueWhenEqual(Pred)); |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Fold comparisons for non-escaping pointer even if the allocation call |
| 2136 | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
| 2137 | // dynamic allocation call could be either of the operands. |
| 2138 | Value *MI = nullptr; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2139 | if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonNullAt(RHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2140 | MI = LHS; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2141 | else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonNullAt(LHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2142 | MI = RHS; |
| 2143 | // FIXME: We should also fold the compare when the pointer escapes, but the |
| 2144 | // compare dominates the pointer escape |
| 2145 | if (MI && !PointerMayBeCaptured(MI, true, true)) |
| 2146 | return ConstantInt::get(GetCompareTy(LHS), |
| 2147 | CmpInst::isFalseWhenEqual(Pred)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2151 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2152 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2153 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2154 | static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, |
| 2155 | Value *RHS) { |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2156 | const APInt *C; |
| 2157 | if (!match(RHS, m_APInt(C))) |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2158 | return nullptr; |
| 2159 | |
| 2160 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2161 | ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, *C); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2162 | if (RHS_CR.isEmptySet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2163 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2164 | if (RHS_CR.isFullSet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2165 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
| 2166 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2167 | // Many binary operators with constant RHS have easy to compute constant |
| 2168 | // range. Use them to check whether the comparison is a tautology. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2169 | unsigned Width = C->getBitWidth(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2170 | APInt Lower = APInt(Width, 0); |
| 2171 | APInt Upper = APInt(Width, 0); |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2172 | const APInt *C2; |
| 2173 | if (match(LHS, m_URem(m_Value(), m_APInt(C2)))) { |
| 2174 | // 'urem x, C2' produces [0, C2). |
| 2175 | Upper = *C2; |
| 2176 | } else if (match(LHS, m_SRem(m_Value(), m_APInt(C2)))) { |
| 2177 | // 'srem x, C2' produces (-|C2|, |C2|). |
| 2178 | Upper = C2->abs(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2179 | Lower = (-Upper) + 1; |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2180 | } else if (match(LHS, m_UDiv(m_APInt(C2), m_Value()))) { |
| 2181 | // 'udiv C2, x' produces [0, C2]. |
| 2182 | Upper = *C2 + 1; |
| 2183 | } else if (match(LHS, m_UDiv(m_Value(), m_APInt(C2)))) { |
| 2184 | // 'udiv x, C2' produces [0, UINT_MAX / C2]. |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2185 | APInt NegOne = APInt::getAllOnesValue(Width); |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2186 | if (*C2 != 0) |
| 2187 | Upper = NegOne.udiv(*C2) + 1; |
| 2188 | } else if (match(LHS, m_SDiv(m_APInt(C2), m_Value()))) { |
| 2189 | if (C2->isMinSignedValue()) { |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2190 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2191 | Lower = *C2; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2192 | Upper = Lower.lshr(1) + 1; |
| 2193 | } else { |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2194 | // 'sdiv C2, x' produces [-|C2|, |C2|]. |
| 2195 | Upper = C2->abs() + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2196 | Lower = (-Upper) + 1; |
| 2197 | } |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2198 | } else if (match(LHS, m_SDiv(m_Value(), m_APInt(C2)))) { |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2199 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2200 | APInt IntMax = APInt::getSignedMaxValue(Width); |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2201 | if (C2->isAllOnesValue()) { |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2202 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2203 | // where C2 != -1 and C2 != 0 and C2 != 1 |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2204 | Lower = IntMin + 1; |
| 2205 | Upper = IntMax + 1; |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2206 | } else if (C2->countLeadingZeros() < Width - 1) { |
| 2207 | // 'sdiv x, C2' produces [INT_MIN / C2, INT_MAX / C2] |
| 2208 | // where C2 != -1 and C2 != 0 and C2 != 1 |
| 2209 | Lower = IntMin.sdiv(*C2); |
| 2210 | Upper = IntMax.sdiv(*C2); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2211 | if (Lower.sgt(Upper)) |
| 2212 | std::swap(Lower, Upper); |
| 2213 | Upper = Upper + 1; |
| 2214 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 2215 | } |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2216 | } else if (match(LHS, m_NUWShl(m_APInt(C2), m_Value()))) { |
| 2217 | // 'shl nuw C2, x' produces [C2, C2 << CLZ(C2)] |
| 2218 | Lower = *C2; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2219 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2220 | } else if (match(LHS, m_NSWShl(m_APInt(C2), m_Value()))) { |
| 2221 | if (C2->isNegative()) { |
| 2222 | // 'shl nsw C2, x' produces [C2 << CLO(C2)-1, C2] |
| 2223 | unsigned ShiftAmount = C2->countLeadingOnes() - 1; |
| 2224 | Lower = C2->shl(ShiftAmount); |
| 2225 | Upper = *C2 + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2226 | } else { |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2227 | // 'shl nsw C2, x' produces [C2, C2 << CLZ(C2)-1] |
| 2228 | unsigned ShiftAmount = C2->countLeadingZeros() - 1; |
| 2229 | Lower = *C2; |
| 2230 | Upper = C2->shl(ShiftAmount) + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2231 | } |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2232 | } else if (match(LHS, m_LShr(m_Value(), m_APInt(C2)))) { |
| 2233 | // 'lshr x, C2' produces [0, UINT_MAX >> C2]. |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2234 | APInt NegOne = APInt::getAllOnesValue(Width); |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2235 | if (C2->ult(Width)) |
| 2236 | Upper = NegOne.lshr(*C2) + 1; |
| 2237 | } else if (match(LHS, m_LShr(m_APInt(C2), m_Value()))) { |
| 2238 | // 'lshr C2, x' produces [C2 >> (Width-1), C2]. |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2239 | unsigned ShiftAmount = Width - 1; |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2240 | if (*C2 != 0 && cast<BinaryOperator>(LHS)->isExact()) |
| 2241 | ShiftAmount = C2->countTrailingZeros(); |
| 2242 | Lower = C2->lshr(ShiftAmount); |
| 2243 | Upper = *C2 + 1; |
| 2244 | } else if (match(LHS, m_AShr(m_Value(), m_APInt(C2)))) { |
| 2245 | // 'ashr x, C2' produces [INT_MIN >> C2, INT_MAX >> C2]. |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2246 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2247 | APInt IntMax = APInt::getSignedMaxValue(Width); |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2248 | if (C2->ult(Width)) { |
| 2249 | Lower = IntMin.ashr(*C2); |
| 2250 | Upper = IntMax.ashr(*C2) + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2251 | } |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2252 | } else if (match(LHS, m_AShr(m_APInt(C2), m_Value()))) { |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2253 | unsigned ShiftAmount = Width - 1; |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2254 | if (*C2 != 0 && cast<BinaryOperator>(LHS)->isExact()) |
| 2255 | ShiftAmount = C2->countTrailingZeros(); |
| 2256 | if (C2->isNegative()) { |
| 2257 | // 'ashr C2, x' produces [C2, C2 >> (Width-1)] |
| 2258 | Lower = *C2; |
| 2259 | Upper = C2->ashr(ShiftAmount) + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2260 | } else { |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2261 | // 'ashr C2, x' produces [C2 >> (Width-1), C2] |
| 2262 | Lower = C2->ashr(ShiftAmount); |
| 2263 | Upper = *C2 + 1; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2264 | } |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2265 | } else if (match(LHS, m_Or(m_Value(), m_APInt(C2)))) { |
| 2266 | // 'or x, C2' produces [C2, UINT_MAX]. |
| 2267 | Lower = *C2; |
| 2268 | } else if (match(LHS, m_And(m_Value(), m_APInt(C2)))) { |
| 2269 | // 'and x, C2' produces [0, C2]. |
| 2270 | Upper = *C2 + 1; |
| 2271 | } else if (match(LHS, m_NUWAdd(m_Value(), m_APInt(C2)))) { |
| 2272 | // 'add nuw x, C2' produces [C2, UINT_MAX]. |
| 2273 | Lower = *C2; |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | ConstantRange LHS_CR = |
| 2277 | Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true); |
| 2278 | |
| 2279 | if (auto *I = dyn_cast<Instruction>(LHS)) |
| 2280 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 2281 | LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges)); |
| 2282 | |
| 2283 | if (!LHS_CR.isFullSet()) { |
| 2284 | if (RHS_CR.contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2285 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2286 | if (RHS_CR.inverse().contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2287 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | return nullptr; |
| 2291 | } |
| 2292 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 2293 | /// Given operands for an ICmpInst, see if we can fold the result. |
| 2294 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 2295 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2296 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 2297 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 2298 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2299 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2300 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 2301 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2302 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 2303 | |
| 2304 | // If we have a constant, make sure it is on the RHS. |
| 2305 | std::swap(LHS, RHS); |
| 2306 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 2307 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2308 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2309 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2310 | Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2312 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 2313 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 2314 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 2315 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2316 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 2317 | |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2318 | // Special case logic when the operands have i1 type. |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 2319 | if (OpTy->getScalarType()->isIntegerTy(1)) { |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2320 | switch (Pred) { |
| 2321 | default: break; |
| 2322 | case ICmpInst::ICMP_EQ: |
| 2323 | // X == 1 -> X |
| 2324 | if (match(RHS, m_One())) |
| 2325 | return LHS; |
| 2326 | break; |
| 2327 | case ICmpInst::ICMP_NE: |
| 2328 | // X != 0 -> X |
| 2329 | if (match(RHS, m_Zero())) |
| 2330 | return LHS; |
| 2331 | break; |
| 2332 | case ICmpInst::ICMP_UGT: |
| 2333 | // X >u 0 -> X |
| 2334 | if (match(RHS, m_Zero())) |
| 2335 | return LHS; |
| 2336 | break; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 2337 | case ICmpInst::ICMP_UGE: { |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2338 | // X >=u 1 -> X |
| 2339 | if (match(RHS, m_One())) |
| 2340 | return LHS; |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 2341 | if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false)) |
Philip Reames | 13f023c | 2015-09-28 17:14:24 +0000 | [diff] [blame] | 2342 | return getTrue(ITy); |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2343 | break; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 2344 | } |
| 2345 | case ICmpInst::ICMP_SGE: { |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 2346 | /// For signed comparison, the values for an i1 are 0 and -1 |
Philip Reames | dbbd779 | 2015-10-29 03:19:10 +0000 | [diff] [blame] | 2347 | /// respectively. This maps into a truth table of: |
| 2348 | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
| 2349 | /// 0 | 0 | 1 (0 >= 0) | 1 |
| 2350 | /// 0 | 1 | 1 (0 >= -1) | 1 |
| 2351 | /// 1 | 0 | 0 (-1 >= 0) | 0 |
| 2352 | /// 1 | 1 | 1 (-1 >= -1) | 1 |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 2353 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
Philip Reames | dbbd779 | 2015-10-29 03:19:10 +0000 | [diff] [blame] | 2354 | return getTrue(ITy); |
| 2355 | break; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 2356 | } |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2357 | case ICmpInst::ICMP_SLT: |
| 2358 | // X <s 0 -> X |
| 2359 | if (match(RHS, m_Zero())) |
| 2360 | return LHS; |
| 2361 | break; |
| 2362 | case ICmpInst::ICMP_SLE: |
| 2363 | // X <=s -1 -> X |
| 2364 | if (match(RHS, m_One())) |
| 2365 | return LHS; |
| 2366 | break; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 2367 | case ICmpInst::ICMP_ULE: { |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 2368 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
Philip Reames | 13f023c | 2015-09-28 17:14:24 +0000 | [diff] [blame] | 2369 | return getTrue(ITy); |
| 2370 | break; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2371 | } |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 2372 | } |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2375 | // If we are comparing with zero then try hard since this is a common case. |
| 2376 | if (match(RHS, m_Zero())) { |
| 2377 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2378 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2379 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2380 | case ICmpInst::ICMP_ULT: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2381 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2382 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2383 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2384 | case ICmpInst::ICMP_EQ: |
| 2385 | case ICmpInst::ICMP_ULE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2386 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2387 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | case ICmpInst::ICMP_NE: |
| 2390 | case ICmpInst::ICMP_UGT: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2391 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2392 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2393 | break; |
| 2394 | case ICmpInst::ICMP_SLT: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2395 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2396 | Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2397 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2398 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2399 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2400 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | case ICmpInst::ICMP_SLE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2403 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2404 | Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2405 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2406 | return getTrue(ITy); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2407 | if (LHSKnownNonNegative && |
| 2408 | isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2409 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | case ICmpInst::ICMP_SGE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2412 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2413 | Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2414 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2415 | return getFalse(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2416 | if (LHSKnownNonNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2417 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | case ICmpInst::ICMP_SGT: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2420 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2421 | Q.CxtI, Q.DT); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2422 | if (LHSKnownNegative) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2423 | return getFalse(ITy); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2424 | if (LHSKnownNonNegative && |
| 2425 | isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2426 | return getTrue(ITy); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2431 | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS)) |
| 2432 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 2433 | |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 2434 | // If both operands have range metadata, use the metadata |
| 2435 | // to simplify the comparison. |
| 2436 | if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) { |
| 2437 | auto RHS_Instr = dyn_cast<Instruction>(RHS); |
| 2438 | auto LHS_Instr = dyn_cast<Instruction>(LHS); |
| 2439 | |
| 2440 | if (RHS_Instr->getMetadata(LLVMContext::MD_range) && |
| 2441 | LHS_Instr->getMetadata(LLVMContext::MD_range)) { |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 2442 | auto RHS_CR = getConstantRangeFromMetadata( |
| 2443 | *RHS_Instr->getMetadata(LLVMContext::MD_range)); |
| 2444 | auto LHS_CR = getConstantRangeFromMetadata( |
| 2445 | *LHS_Instr->getMetadata(LLVMContext::MD_range)); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 2446 | |
| 2447 | auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); |
| 2448 | if (Satisfied_CR.contains(LHS_CR)) |
| 2449 | return ConstantInt::getTrue(RHS->getContext()); |
| 2450 | |
| 2451 | auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( |
| 2452 | CmpInst::getInversePredicate(Pred), RHS_CR); |
| 2453 | if (InversedSatisfied_CR.contains(LHS_CR)) |
| 2454 | return ConstantInt::getFalse(RHS->getContext()); |
| 2455 | } |
| 2456 | } |
| 2457 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2458 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 2459 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 2460 | Instruction *LI = cast<CastInst>(LHS); |
| 2461 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2462 | Type *SrcTy = SrcOp->getType(); |
| 2463 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 2466 | // if the integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2467 | if (MaxRecurse && isa<PtrToIntInst>(LI) && |
| 2468 | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2469 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2470 | // Transfer the cast to the constant. |
| 2471 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 2472 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2473 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2474 | return V; |
| 2475 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 2476 | if (RI->getOperand(0)->getType() == SrcTy) |
| 2477 | // Compare without the cast. |
| 2478 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2479 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2480 | return V; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | if (isa<ZExtInst>(LHS)) { |
| 2485 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 2486 | // same type. |
| 2487 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 2488 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2489 | // Compare X and Y. Note that signed predicates become unsigned. |
| 2490 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2491 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2492 | MaxRecurse-1)) |
| 2493 | return V; |
| 2494 | } |
| 2495 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 2496 | // too. If not, then try to deduce the result of the comparison. |
| 2497 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2498 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2499 | // reextended to DstTy. |
| 2500 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2501 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 2502 | |
| 2503 | // If the re-extended constant didn't change then this is effectively |
| 2504 | // also a case of comparing two zero-extended values. |
| 2505 | if (RExt == CI && MaxRecurse) |
| 2506 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2507 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2508 | return V; |
| 2509 | |
| 2510 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 2511 | // there. Use this to work out the result of the comparison. |
| 2512 | if (RExt != CI) { |
| 2513 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2514 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2515 | // LHS <u RHS. |
| 2516 | case ICmpInst::ICMP_EQ: |
| 2517 | case ICmpInst::ICMP_UGT: |
| 2518 | case ICmpInst::ICMP_UGE: |
| 2519 | return ConstantInt::getFalse(CI->getContext()); |
| 2520 | |
| 2521 | case ICmpInst::ICMP_NE: |
| 2522 | case ICmpInst::ICMP_ULT: |
| 2523 | case ICmpInst::ICMP_ULE: |
| 2524 | return ConstantInt::getTrue(CI->getContext()); |
| 2525 | |
| 2526 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 2527 | // is non-negative then LHS <s RHS. |
| 2528 | case ICmpInst::ICMP_SGT: |
| 2529 | case ICmpInst::ICMP_SGE: |
| 2530 | return CI->getValue().isNegative() ? |
| 2531 | ConstantInt::getTrue(CI->getContext()) : |
| 2532 | ConstantInt::getFalse(CI->getContext()); |
| 2533 | |
| 2534 | case ICmpInst::ICMP_SLT: |
| 2535 | case ICmpInst::ICMP_SLE: |
| 2536 | return CI->getValue().isNegative() ? |
| 2537 | ConstantInt::getFalse(CI->getContext()) : |
| 2538 | ConstantInt::getTrue(CI->getContext()); |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | if (isa<SExtInst>(LHS)) { |
| 2545 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 2546 | // same type. |
| 2547 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 2548 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 2549 | // Compare X and Y. Note that the predicate does not change. |
| 2550 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2551 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2552 | return V; |
| 2553 | } |
| 2554 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 2555 | // too. If not, then try to deduce the result of the comparison. |
| 2556 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2557 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2558 | // reextended to DstTy. |
| 2559 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 2560 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 2561 | |
| 2562 | // If the re-extended constant didn't change then this is effectively |
| 2563 | // also a case of comparing two sign-extended values. |
| 2564 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2565 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2566 | return V; |
| 2567 | |
| 2568 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 2569 | // bits there. Use this to work out the result of the comparison. |
| 2570 | if (RExt != CI) { |
| 2571 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 2572 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2573 | case ICmpInst::ICMP_EQ: |
| 2574 | return ConstantInt::getFalse(CI->getContext()); |
| 2575 | case ICmpInst::ICMP_NE: |
| 2576 | return ConstantInt::getTrue(CI->getContext()); |
| 2577 | |
| 2578 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 2579 | // LHS >s RHS. |
| 2580 | case ICmpInst::ICMP_SGT: |
| 2581 | case ICmpInst::ICMP_SGE: |
| 2582 | return CI->getValue().isNegative() ? |
| 2583 | ConstantInt::getTrue(CI->getContext()) : |
| 2584 | ConstantInt::getFalse(CI->getContext()); |
| 2585 | case ICmpInst::ICMP_SLT: |
| 2586 | case ICmpInst::ICMP_SLE: |
| 2587 | return CI->getValue().isNegative() ? |
| 2588 | ConstantInt::getFalse(CI->getContext()) : |
| 2589 | ConstantInt::getTrue(CI->getContext()); |
| 2590 | |
| 2591 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 2592 | // LHS >u RHS. |
| 2593 | case ICmpInst::ICMP_UGT: |
| 2594 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2595 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2596 | if (MaxRecurse) |
| 2597 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 2598 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2599 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2600 | return V; |
| 2601 | break; |
| 2602 | case ICmpInst::ICMP_ULT: |
| 2603 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2604 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2605 | if (MaxRecurse) |
| 2606 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 2607 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2608 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2609 | return V; |
| 2610 | break; |
| 2611 | } |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2617 | // icmp eq|ne X, Y -> false|true if X != Y |
| 2618 | if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && |
| 2619 | isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) { |
| 2620 | LLVMContext &Ctx = LHS->getType()->getContext(); |
| 2621 | return Pred == ICmpInst::ICMP_NE ? |
| 2622 | ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx); |
| 2623 | } |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 2624 | |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2625 | // Special logic for binary operators. |
| 2626 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2627 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2628 | if (MaxRecurse && (LBO || RBO)) { |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2629 | // Analyze the case when either LHS or RHS is an add instruction. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2630 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2631 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2632 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2633 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2634 | A = LBO->getOperand(0); B = LBO->getOperand(1); |
| 2635 | NoLHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2636 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2637 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2638 | } |
| 2639 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2640 | C = RBO->getOperand(0); D = RBO->getOperand(1); |
| 2641 | NoRHSWrapProblem = ICmpInst::isEquality(Pred) || |
| 2642 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2643 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2644 | } |
| 2645 | |
| 2646 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2647 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2648 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2649 | Constant::getNullValue(RHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2650 | Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2651 | return V; |
| 2652 | |
| 2653 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2654 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2655 | if (Value *V = SimplifyICmpInst(Pred, |
| 2656 | Constant::getNullValue(LHS->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2657 | C == LHS ? D : C, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2658 | return V; |
| 2659 | |
| 2660 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2661 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 2662 | NoLHSWrapProblem && NoRHSWrapProblem) { |
| 2663 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2664 | Value *Y, *Z; |
| 2665 | if (A == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2666 | // C + B == C + D -> B == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2667 | Y = B; |
| 2668 | Z = D; |
| 2669 | } else if (A == D) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2670 | // D + B == C + D -> B == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2671 | Y = B; |
| 2672 | Z = C; |
| 2673 | } else if (B == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2674 | // A + C == C + D -> A == D |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2675 | Y = A; |
| 2676 | Z = D; |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 2677 | } else { |
| 2678 | assert(B == D); |
| 2679 | // A + D == C + D -> A == C |
Duncan Sands | c41076c | 2012-11-16 19:41:26 +0000 | [diff] [blame] | 2680 | Y = A; |
| 2681 | Z = C; |
| 2682 | } |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2683 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 2684 | return V; |
| 2685 | } |
| 2686 | } |
| 2687 | |
Nick Lewycky | 762f8a8 | 2016-04-21 00:53:14 +0000 | [diff] [blame] | 2688 | { |
| 2689 | Value *Y = nullptr; |
| 2690 | // icmp pred (or X, Y), X |
| 2691 | if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
| 2692 | if (Pred == ICmpInst::ICMP_ULT) |
| 2693 | return getFalse(ITy); |
| 2694 | if (Pred == ICmpInst::ICMP_UGE) |
| 2695 | return getTrue(ITy); |
| 2696 | |
| 2697 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { |
| 2698 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 2699 | bool YKnownNonNegative, YKnownNegative; |
| 2700 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0, |
| 2701 | Q.AC, Q.CxtI, Q.DT); |
| 2702 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
| 2703 | Q.CxtI, Q.DT); |
| 2704 | if (RHSKnownNonNegative && YKnownNegative) |
| 2705 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
| 2706 | if (RHSKnownNegative || YKnownNonNegative) |
| 2707 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy); |
| 2708 | } |
| 2709 | } |
| 2710 | // icmp pred X, (or X, Y) |
| 2711 | if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) { |
| 2712 | if (Pred == ICmpInst::ICMP_ULE) |
| 2713 | return getTrue(ITy); |
| 2714 | if (Pred == ICmpInst::ICMP_UGT) |
| 2715 | return getFalse(ITy); |
| 2716 | |
| 2717 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) { |
| 2718 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2719 | bool YKnownNonNegative, YKnownNegative; |
| 2720 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, |
| 2721 | Q.AC, Q.CxtI, Q.DT); |
| 2722 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
| 2723 | Q.CxtI, Q.DT); |
| 2724 | if (LHSKnownNonNegative && YKnownNegative) |
| 2725 | return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy); |
| 2726 | if (LHSKnownNegative || YKnownNonNegative) |
| 2727 | return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy); |
| 2728 | } |
| 2729 | } |
David Majnemer | bd9ce4e | 2014-11-25 02:55:48 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | // icmp pred (and X, Y), X |
| 2733 | if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)), |
| 2734 | m_And(m_Specific(RHS), m_Value())))) { |
| 2735 | if (Pred == ICmpInst::ICMP_UGT) |
| 2736 | return getFalse(ITy); |
| 2737 | if (Pred == ICmpInst::ICMP_ULE) |
| 2738 | return getTrue(ITy); |
| 2739 | } |
| 2740 | // icmp pred X, (and X, Y) |
| 2741 | if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)), |
| 2742 | m_And(m_Specific(LHS), m_Value())))) { |
| 2743 | if (Pred == ICmpInst::ICMP_UGE) |
| 2744 | return getTrue(ITy); |
| 2745 | if (Pred == ICmpInst::ICMP_ULT) |
| 2746 | return getFalse(ITy); |
| 2747 | } |
| 2748 | |
David Majnemer | 2d6c023 | 2014-05-14 20:16:28 +0000 | [diff] [blame] | 2749 | // 0 - (zext X) pred C |
| 2750 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2751 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2752 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2753 | if (Pred == ICmpInst::ICMP_SLT) |
| 2754 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2755 | if (Pred == ICmpInst::ICMP_SGE) |
| 2756 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2757 | if (Pred == ICmpInst::ICMP_EQ) |
| 2758 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2759 | if (Pred == ICmpInst::ICMP_NE) |
| 2760 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2761 | } |
| 2762 | if (RHSC->getValue().isNonNegative()) { |
| 2763 | if (Pred == ICmpInst::ICMP_SLE) |
| 2764 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2765 | if (Pred == ICmpInst::ICMP_SGT) |
| 2766 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2771 | // icmp pred (urem X, Y), Y |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2772 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2773 | bool KnownNonNegative, KnownNegative; |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2774 | switch (Pred) { |
| 2775 | default: |
| 2776 | break; |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2777 | case ICmpInst::ICMP_SGT: |
| 2778 | case ICmpInst::ICMP_SGE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2779 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2780 | Q.CxtI, Q.DT); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2781 | if (!KnownNonNegative) |
| 2782 | break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2783 | LLVM_FALLTHROUGH; |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2784 | case ICmpInst::ICMP_EQ: |
| 2785 | case ICmpInst::ICMP_UGT: |
| 2786 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2787 | return getFalse(ITy); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2788 | case ICmpInst::ICMP_SLT: |
| 2789 | case ICmpInst::ICMP_SLE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2790 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2791 | Q.CxtI, Q.DT); |
Nick Lewycky | 8e3a79d | 2011-03-04 10:06:52 +0000 | [diff] [blame] | 2792 | if (!KnownNonNegative) |
| 2793 | break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2794 | LLVM_FALLTHROUGH; |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2795 | case ICmpInst::ICMP_NE: |
| 2796 | case ICmpInst::ICMP_ULT: |
| 2797 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2798 | return getTrue(ITy); |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2799 | } |
| 2800 | } |
Nick Lewycky | 35aeea9 | 2013-07-12 23:42:57 +0000 | [diff] [blame] | 2801 | |
| 2802 | // icmp pred X, (urem Y, X) |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2803 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2804 | bool KnownNonNegative, KnownNegative; |
| 2805 | switch (Pred) { |
| 2806 | default: |
| 2807 | break; |
| 2808 | case ICmpInst::ICMP_SGT: |
| 2809 | case ICmpInst::ICMP_SGE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2810 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2811 | Q.CxtI, Q.DT); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2812 | if (!KnownNonNegative) |
| 2813 | break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2814 | LLVM_FALLTHROUGH; |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2815 | case ICmpInst::ICMP_NE: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2816 | case ICmpInst::ICMP_UGT: |
| 2817 | case ICmpInst::ICMP_UGE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2818 | return getTrue(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2819 | case ICmpInst::ICMP_SLT: |
| 2820 | case ICmpInst::ICMP_SLE: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2821 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2822 | Q.CxtI, Q.DT); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2823 | if (!KnownNonNegative) |
| 2824 | break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2825 | LLVM_FALLTHROUGH; |
Nick Lewycky | 774647d | 2011-03-09 08:20:06 +0000 | [diff] [blame] | 2826 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2827 | case ICmpInst::ICMP_ULT: |
| 2828 | case ICmpInst::ICMP_ULE: |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2829 | return getFalse(ITy); |
Nick Lewycky | 980104d | 2011-03-09 06:26:03 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
Nick Lewycky | c9d2006 | 2011-03-01 08:15:50 +0000 | [diff] [blame] | 2832 | |
David Majnemer | 3af5bf3 | 2016-01-21 18:55:54 +0000 | [diff] [blame] | 2833 | // x >> y <=u x |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2834 | // x udiv y <=u x. |
David Majnemer | 3af5bf3 | 2016-01-21 18:55:54 +0000 | [diff] [blame] | 2835 | if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) || |
| 2836 | match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) { |
| 2837 | // icmp pred (X op Y), X |
Duncan Sands | 92af0a8 | 2011-10-28 18:17:44 +0000 | [diff] [blame] | 2838 | if (Pred == ICmpInst::ICMP_UGT) |
| 2839 | return getFalse(ITy); |
| 2840 | if (Pred == ICmpInst::ICMP_ULE) |
| 2841 | return getTrue(ITy); |
| 2842 | } |
| 2843 | |
David Majnemer | 76d06bc | 2014-08-28 03:34:28 +0000 | [diff] [blame] | 2844 | // handle: |
| 2845 | // CI2 << X == CI |
| 2846 | // CI2 << X != CI |
| 2847 | // |
| 2848 | // where CI2 is a power of 2 and CI isn't |
| 2849 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2850 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2851 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2852 | CI2Val->isPowerOf2()) { |
| 2853 | if (!CIVal->isPowerOf2()) { |
| 2854 | // CI2 << X can equal zero in some circumstances, |
| 2855 | // this simplification is unsafe if CI is zero. |
| 2856 | // |
| 2857 | // We know it is safe if: |
| 2858 | // - The shift is nsw, we can't shift out the one bit. |
| 2859 | // - The shift is nuw, we can't shift out the one bit. |
| 2860 | // - CI2 is one |
| 2861 | // - CI isn't zero |
| 2862 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
| 2863 | *CI2Val == 1 || !CI->isZero()) { |
| 2864 | if (Pred == ICmpInst::ICMP_EQ) |
| 2865 | return ConstantInt::getFalse(RHS->getContext()); |
| 2866 | if (Pred == ICmpInst::ICMP_NE) |
| 2867 | return ConstantInt::getTrue(RHS->getContext()); |
| 2868 | } |
| 2869 | } |
| 2870 | if (CIVal->isSignBit() && *CI2Val == 1) { |
| 2871 | if (Pred == ICmpInst::ICMP_UGT) |
| 2872 | return ConstantInt::getFalse(RHS->getContext()); |
| 2873 | if (Pred == ICmpInst::ICMP_ULE) |
| 2874 | return ConstantInt::getTrue(RHS->getContext()); |
| 2875 | } |
| 2876 | } |
| 2877 | } |
| 2878 | |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2879 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2880 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2881 | switch (LBO->getOpcode()) { |
| 2882 | default: break; |
| 2883 | case Instruction::UDiv: |
| 2884 | case Instruction::LShr: |
| 2885 | if (ICmpInst::isSigned(Pred)) |
| 2886 | break; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2887 | LLVM_FALLTHROUGH; |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2888 | case Instruction::SDiv: |
| 2889 | case Instruction::AShr: |
Eli Friedman | 8a20e66 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2890 | if (!LBO->isExact() || !RBO->isExact()) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2891 | break; |
| 2892 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2893 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2894 | return V; |
| 2895 | break; |
| 2896 | case Instruction::Shl: { |
Duncan Sands | 020c194 | 2011-08-04 10:02:21 +0000 | [diff] [blame] | 2897 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2898 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2899 | if (!NUW && !NSW) |
| 2900 | break; |
| 2901 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2902 | break; |
| 2903 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2904 | RBO->getOperand(0), Q, MaxRecurse-1)) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2905 | return V; |
| 2906 | break; |
| 2907 | } |
| 2908 | } |
| 2909 | } |
| 2910 | |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2911 | // Simplify comparisons involving max/min. |
| 2912 | Value *A, *B; |
| 2913 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2914 | 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] | 2915 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2916 | // Signed variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2917 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2918 | if (A != RHS) std::swap(A, B); // smax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2919 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2920 | // We analyze this as smax(A, B) pred A. |
| 2921 | P = Pred; |
| 2922 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2923 | (A == LHS || B == LHS)) { |
| 2924 | if (A != LHS) std::swap(A, B); // A pred smax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2925 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2926 | // We analyze this as smax(A, B) swapped-pred A. |
| 2927 | P = CmpInst::getSwappedPredicate(Pred); |
| 2928 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2929 | (A == RHS || B == RHS)) { |
| 2930 | if (A != RHS) std::swap(A, B); // smin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2931 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2932 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2933 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2934 | P = CmpInst::getSwappedPredicate(Pred); |
| 2935 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2936 | (A == LHS || B == LHS)) { |
| 2937 | if (A != LHS) std::swap(A, B); // A pred smin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2938 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2939 | // We analyze this as smax(-A, -B) pred -A. |
| 2940 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2941 | P = Pred; |
| 2942 | } |
| 2943 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2944 | // Cases correspond to "max(A, B) p A". |
| 2945 | switch (P) { |
| 2946 | default: |
| 2947 | break; |
| 2948 | case CmpInst::ICMP_EQ: |
| 2949 | case CmpInst::ICMP_SLE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2950 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2951 | // in the max/min; if so, we can just return that. |
| 2952 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2953 | return V; |
| 2954 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2955 | return V; |
| 2956 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2957 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2958 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2959 | return V; |
| 2960 | break; |
| 2961 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2962 | case CmpInst::ICMP_SGT: { |
| 2963 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2964 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2965 | // tested in the max/min; if so, we can just return that. |
| 2966 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2967 | return V; |
| 2968 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2969 | return V; |
| 2970 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2971 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2972 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2973 | return V; |
| 2974 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2975 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2976 | case CmpInst::ICMP_SGE: |
| 2977 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2978 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2979 | case CmpInst::ICMP_SLT: |
| 2980 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 2981 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
| 2984 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 2985 | // Unsigned variants on "max(a,b)>=a -> true". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2986 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2987 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2988 | if (A != RHS) std::swap(A, B); // umax(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2989 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2990 | // We analyze this as umax(A, B) pred A. |
| 2991 | P = Pred; |
| 2992 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2993 | (A == LHS || B == LHS)) { |
| 2994 | if (A != LHS) std::swap(A, B); // A pred umax(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2995 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 2996 | // We analyze this as umax(A, B) swapped-pred A. |
| 2997 | P = CmpInst::getSwappedPredicate(Pred); |
| 2998 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2999 | (A == RHS || B == RHS)) { |
| 3000 | if (A != RHS) std::swap(A, B); // umin(A, B) pred A. |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3001 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3002 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 3003 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 3004 | P = CmpInst::getSwappedPredicate(Pred); |
| 3005 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3006 | (A == LHS || B == LHS)) { |
| 3007 | if (A != LHS) std::swap(A, B); // A pred umin(A, B). |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3008 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3009 | // We analyze this as umax(-A, -B) pred -A. |
| 3010 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 3011 | P = Pred; |
| 3012 | } |
| 3013 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 3014 | // Cases correspond to "max(A, B) p A". |
| 3015 | switch (P) { |
| 3016 | default: |
| 3017 | break; |
| 3018 | case CmpInst::ICMP_EQ: |
| 3019 | case CmpInst::ICMP_ULE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 3020 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 3021 | // in the max/min; if so, we can just return that. |
| 3022 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 3023 | return V; |
| 3024 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 3025 | return V; |
| 3026 | // Otherwise, see if "A EqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3027 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3028 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3029 | return V; |
| 3030 | break; |
| 3031 | case CmpInst::ICMP_NE: |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 3032 | case CmpInst::ICMP_UGT: { |
| 3033 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 3034 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 3035 | // tested in the max/min; if so, we can just return that. |
| 3036 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 3037 | return V; |
| 3038 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 3039 | return V; |
| 3040 | // Otherwise, see if "A InvEqP B" simplifies. |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3041 | if (MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3042 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse-1)) |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3043 | return V; |
| 3044 | break; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 3045 | } |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3046 | case CmpInst::ICMP_UGE: |
| 3047 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3048 | return getTrue(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3049 | case CmpInst::ICMP_ULT: |
| 3050 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3051 | return getFalse(ITy); |
Duncan Sands | 0a9c124 | 2011-05-03 19:53:10 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3055 | // Variants on "max(x,y) >= min(x,z)". |
| 3056 | Value *C, *D; |
| 3057 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 3058 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 3059 | (A == C || A == D || B == C || B == D)) { |
| 3060 | // max(x, ?) pred min(x, ?). |
| 3061 | if (Pred == CmpInst::ICMP_SGE) |
| 3062 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3063 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3064 | if (Pred == CmpInst::ICMP_SLT) |
| 3065 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3066 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3067 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 3068 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 3069 | (A == C || A == D || B == C || B == D)) { |
| 3070 | // min(x, ?) pred max(x, ?). |
| 3071 | if (Pred == CmpInst::ICMP_SLE) |
| 3072 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3073 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3074 | if (Pred == CmpInst::ICMP_SGT) |
| 3075 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3076 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3077 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 3078 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 3079 | (A == C || A == D || B == C || B == D)) { |
| 3080 | // max(x, ?) pred min(x, ?). |
| 3081 | if (Pred == CmpInst::ICMP_UGE) |
| 3082 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3083 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3084 | if (Pred == CmpInst::ICMP_ULT) |
| 3085 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3086 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3087 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3088 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 3089 | (A == C || A == D || B == C || B == D)) { |
| 3090 | // min(x, ?) pred max(x, ?). |
| 3091 | if (Pred == CmpInst::ICMP_ULE) |
| 3092 | // Always true. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3093 | return getTrue(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3094 | if (Pred == CmpInst::ICMP_UGT) |
| 3095 | // Always false. |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 3096 | return getFalse(ITy); |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3099 | // Simplify comparisons of related pointers using a powerful, recursive |
| 3100 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 3101 | if (LHS->getType()->isPointerTy()) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 3102 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, LHS, RHS)) |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3103 | return C; |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3104 | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
| 3105 | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
| 3106 | if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
| 3107 | Q.DL.getTypeSizeInBits(CLHS->getType()) && |
| 3108 | Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) == |
| 3109 | Q.DL.getTypeSizeInBits(CRHS->getType())) |
| 3110 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, |
| 3111 | CLHS->getPointerOperand(), |
| 3112 | CRHS->getPointerOperand())) |
| 3113 | return C; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3114 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3115 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 3116 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 3117 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 3118 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 3119 | (ICmpInst::isEquality(Pred) || |
| 3120 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 3121 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 3122 | // The bases are equal and the indices are constant. Build a constant |
| 3123 | // expression GEP with the same indices and a null base pointer to see |
| 3124 | // what constant folding can make out of it. |
| 3125 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 3126 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3127 | Constant *NewLHS = ConstantExpr::getGetElementPtr( |
| 3128 | GLHS->getSourceElementType(), Null, IndicesLHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3129 | |
| 3130 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3131 | Constant *NewRHS = ConstantExpr::getGetElementPtr( |
| 3132 | GLHS->getSourceElementType(), Null, IndicesRHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3133 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 3134 | } |
| 3135 | } |
| 3136 | } |
| 3137 | |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3138 | // If a bit is known to be zero for A and known to be one for B, |
| 3139 | // then A and B cannot be equal. |
| 3140 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3141 | const APInt *RHSVal; |
| 3142 | if (match(RHS, m_APInt(RHSVal))) { |
| 3143 | unsigned BitWidth = RHSVal->getBitWidth(); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3144 | APInt LHSKnownZero(BitWidth, 0); |
| 3145 | APInt LHSKnownOne(BitWidth, 0); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3146 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC, |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3147 | Q.CxtI, Q.DT); |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3148 | if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0)) |
| 3149 | return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy) |
| 3150 | : ConstantInt::getTrue(ITy); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3154 | // If the comparison is with the result of a select instruction, check whether |
| 3155 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3156 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3157 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3158 | return V; |
| 3159 | |
| 3160 | // If the comparison is with the result of a phi instruction, check whether |
| 3161 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3162 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3163 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3164 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3165 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3166 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3169 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3170 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3171 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3172 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 85dbea9 | 2015-12-24 09:08:08 +0000 | [diff] [blame] | 3173 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3174 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3175 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3178 | /// Given operands for an FCmpInst, see if we can fold the result. |
| 3179 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3180 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3181 | FastMathFlags FMF, const Query &Q, |
| 3182 | unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3183 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 3184 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 3185 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3186 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3187 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3188 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3190 | // If we have a constant, make sure it is on the RHS. |
| 3191 | std::swap(LHS, RHS); |
| 3192 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3193 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3194 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3195 | // Fold trivial predicates. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3196 | Type *RetTy = GetCompareTy(LHS); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3197 | if (Pred == FCmpInst::FCMP_FALSE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3198 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3199 | if (Pred == FCmpInst::FCMP_TRUE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3200 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3201 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3202 | // UNO/ORD predicates can be trivially folded if NaNs are ignored. |
| 3203 | if (FMF.noNaNs()) { |
| 3204 | if (Pred == FCmpInst::FCMP_UNO) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3205 | return getFalse(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3206 | if (Pred == FCmpInst::FCMP_ORD) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3207 | return getTrue(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3210 | // fcmp pred x, undef and fcmp pred undef, x |
| 3211 | // fold to true if unordered, false if ordered |
| 3212 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) { |
| 3213 | // Choosing NaN for the undef will always make unordered comparison succeed |
| 3214 | // and ordered comparison fail. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3215 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3216 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3217 | |
| 3218 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3219 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3220 | if (CmpInst::isTrueWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3221 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3222 | if (CmpInst::isFalseWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3223 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3224 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3225 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3226 | // Handle fcmp with constant RHS |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3227 | const ConstantFP *CFP = nullptr; |
| 3228 | if (const auto *RHSC = dyn_cast<Constant>(RHS)) { |
| 3229 | if (RHS->getType()->isVectorTy()) |
| 3230 | CFP = dyn_cast_or_null<ConstantFP>(RHSC->getSplatValue()); |
| 3231 | else |
| 3232 | CFP = dyn_cast<ConstantFP>(RHSC); |
| 3233 | } |
| 3234 | if (CFP) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3235 | // If the constant is a nan, see if we can fold the comparison based on it. |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3236 | if (CFP->getValueAPF().isNaN()) { |
| 3237 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3238 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3239 | assert(FCmpInst::isUnordered(Pred) && |
| 3240 | "Comparison must be either ordered or unordered!"); |
| 3241 | // True if unordered. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3242 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3243 | } |
| 3244 | // Check whether the constant is an infinity. |
| 3245 | if (CFP->getValueAPF().isInfinity()) { |
| 3246 | if (CFP->getValueAPF().isNegative()) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3247 | switch (Pred) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3248 | case FCmpInst::FCMP_OLT: |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3249 | // No value is ordered and less than negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3250 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3251 | case FCmpInst::FCMP_UGE: |
| 3252 | // All values are unordered with or at least negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3253 | return getTrue(RetTy); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3254 | default: |
| 3255 | break; |
| 3256 | } |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3257 | } else { |
| 3258 | switch (Pred) { |
| 3259 | case FCmpInst::FCMP_OGT: |
| 3260 | // No value is ordered and greater than infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3261 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3262 | case FCmpInst::FCMP_ULE: |
| 3263 | // All values are unordered with and at most infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3264 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3265 | default: |
| 3266 | break; |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | if (CFP->getValueAPF().isZero()) { |
| 3271 | switch (Pred) { |
| 3272 | case FCmpInst::FCMP_UGE: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3273 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3274 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3275 | break; |
| 3276 | case FCmpInst::FCMP_OLT: |
| 3277 | // X < 0 |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3278 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3279 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3280 | break; |
| 3281 | default: |
| 3282 | break; |
| 3283 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3286 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3287 | // If the comparison is with the result of a select instruction, check whether |
| 3288 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3289 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3290 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3291 | return V; |
| 3292 | |
| 3293 | // If the comparison is with the result of a phi instruction, check whether |
| 3294 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3295 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3296 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3297 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3298 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3299 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3302 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3303 | FastMathFlags FMF, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3304 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3305 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3306 | const Instruction *CxtI) { |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3307 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, |
| 3308 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3311 | /// See if V simplifies when its operand Op is replaced with RepOp. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3312 | static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
| 3313 | const Query &Q, |
| 3314 | unsigned MaxRecurse) { |
| 3315 | // Trivial replacement. |
| 3316 | if (V == Op) |
| 3317 | return RepOp; |
| 3318 | |
| 3319 | auto *I = dyn_cast<Instruction>(V); |
| 3320 | if (!I) |
| 3321 | return nullptr; |
| 3322 | |
| 3323 | // If this is a binary operator, try to simplify it with the replaced op. |
| 3324 | if (auto *B = dyn_cast<BinaryOperator>(I)) { |
| 3325 | // Consider: |
| 3326 | // %cmp = icmp eq i32 %x, 2147483647 |
| 3327 | // %add = add nsw i32 %x, 1 |
| 3328 | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
| 3329 | // |
| 3330 | // We can't replace %sel with %add unless we strip away the flags. |
| 3331 | if (isa<OverflowingBinaryOperator>(B)) |
| 3332 | if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap()) |
| 3333 | return nullptr; |
| 3334 | if (isa<PossiblyExactOperator>(B)) |
| 3335 | if (B->isExact()) |
| 3336 | return nullptr; |
| 3337 | |
| 3338 | if (MaxRecurse) { |
| 3339 | if (B->getOperand(0) == Op) |
| 3340 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q, |
| 3341 | MaxRecurse - 1); |
| 3342 | if (B->getOperand(1) == Op) |
| 3343 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q, |
| 3344 | MaxRecurse - 1); |
| 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | // Same for CmpInsts. |
| 3349 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 3350 | if (MaxRecurse) { |
| 3351 | if (C->getOperand(0) == Op) |
| 3352 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q, |
| 3353 | MaxRecurse - 1); |
| 3354 | if (C->getOperand(1) == Op) |
| 3355 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q, |
| 3356 | MaxRecurse - 1); |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | // TODO: We could hand off more cases to instsimplify here. |
| 3361 | |
| 3362 | // If all operands are constant after substituting Op for RepOp then we can |
| 3363 | // constant fold the instruction. |
| 3364 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 3365 | // Build a list of all constant operands. |
| 3366 | SmallVector<Constant *, 8> ConstOps; |
| 3367 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3368 | if (I->getOperand(i) == Op) |
| 3369 | ConstOps.push_back(CRepOp); |
| 3370 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 3371 | ConstOps.push_back(COp); |
| 3372 | else |
| 3373 | break; |
| 3374 | } |
| 3375 | |
| 3376 | // All operands were constants, fold it. |
| 3377 | if (ConstOps.size() == I->getNumOperands()) { |
| 3378 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 3379 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 3380 | ConstOps[1], Q.DL, Q.TLI); |
| 3381 | |
| 3382 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 3383 | if (!LI->isVolatile()) |
Eduard Burtescu | 1423921 | 2016-01-22 01:17:26 +0000 | [diff] [blame] | 3384 | return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3385 | |
Manuel Jacob | e902459 | 2016-01-21 06:33:22 +0000 | [diff] [blame] | 3386 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | return nullptr; |
| 3391 | } |
| 3392 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3393 | /// Try to simplify a select instruction when its condition operand is an |
| 3394 | /// integer comparison where one operand of the compare is a constant. |
| 3395 | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
| 3396 | const APInt *Y, bool TrueWhenUnset) { |
| 3397 | const APInt *C; |
| 3398 | |
| 3399 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3400 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3401 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3402 | *Y == ~*C) |
| 3403 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3404 | |
| 3405 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3406 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3407 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3408 | *Y == ~*C) |
| 3409 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3410 | |
| 3411 | if (Y->isPowerOf2()) { |
| 3412 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3413 | // (X & Y) != 0 ? X | Y : X --> X |
| 3414 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3415 | *Y == *C) |
| 3416 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3417 | |
| 3418 | // (X & Y) == 0 ? X : X | Y --> X |
| 3419 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3420 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3421 | *Y == *C) |
| 3422 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3423 | } |
| 3424 | |
| 3425 | return nullptr; |
| 3426 | } |
| 3427 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3428 | /// An alternative way to test if a bit is set or not uses sgt/slt instead of |
| 3429 | /// eq/ne. |
| 3430 | static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *TrueVal, |
| 3431 | Value *FalseVal, |
| 3432 | bool TrueWhenUnset) { |
| 3433 | unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); |
Sanjay Patel | e9fc79b | 2016-07-21 21:56:00 +0000 | [diff] [blame] | 3434 | if (!BitWidth) |
| 3435 | return nullptr; |
| 3436 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3437 | APInt MinSignedValue; |
| 3438 | Value *X; |
| 3439 | if (match(CmpLHS, m_Trunc(m_Value(X))) && (X == TrueVal || X == FalseVal)) { |
| 3440 | // icmp slt (trunc X), 0 <--> icmp ne (and X, C), 0 |
| 3441 | // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0 |
| 3442 | unsigned DestSize = CmpLHS->getType()->getScalarSizeInBits(); |
| 3443 | MinSignedValue = APInt::getSignedMinValue(DestSize).zext(BitWidth); |
| 3444 | } else { |
| 3445 | // icmp slt X, 0 <--> icmp ne (and X, C), 0 |
| 3446 | // icmp sgt X, -1 <--> icmp eq (and X, C), 0 |
| 3447 | X = CmpLHS; |
| 3448 | MinSignedValue = APInt::getSignedMinValue(BitWidth); |
| 3449 | } |
| 3450 | |
| 3451 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, &MinSignedValue, |
| 3452 | TrueWhenUnset)) |
| 3453 | return V; |
| 3454 | |
| 3455 | return nullptr; |
| 3456 | } |
| 3457 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3458 | /// Try to simplify a select instruction when its condition operand is an |
| 3459 | /// integer comparison. |
| 3460 | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
| 3461 | Value *FalseVal, const Query &Q, |
| 3462 | unsigned MaxRecurse) { |
| 3463 | ICmpInst::Predicate Pred; |
| 3464 | Value *CmpLHS, *CmpRHS; |
| 3465 | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
| 3466 | return nullptr; |
| 3467 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 3468 | // FIXME: This code is nearly duplicated in InstCombine. Using/refactoring |
| 3469 | // decomposeBitTestICmp() might help. |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3470 | if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) { |
| 3471 | Value *X; |
| 3472 | const APInt *Y; |
| 3473 | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
| 3474 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
| 3475 | Pred == ICmpInst::ICMP_EQ)) |
| 3476 | return V; |
| 3477 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3478 | // Comparing signed-less-than 0 checks if the sign bit is set. |
| 3479 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3480 | false)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3481 | return V; |
| 3482 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3483 | // Comparing signed-greater-than -1 checks if the sign bit is not set. |
| 3484 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3485 | true)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3486 | return V; |
| 3487 | } |
| 3488 | |
| 3489 | if (CondVal->hasOneUse()) { |
| 3490 | const APInt *C; |
| 3491 | if (match(CmpRHS, m_APInt(C))) { |
| 3492 | // X < MIN ? T : F --> F |
| 3493 | if (Pred == ICmpInst::ICMP_SLT && C->isMinSignedValue()) |
| 3494 | return FalseVal; |
| 3495 | // X < MIN ? T : F --> F |
| 3496 | if (Pred == ICmpInst::ICMP_ULT && C->isMinValue()) |
| 3497 | return FalseVal; |
| 3498 | // X > MAX ? T : F --> F |
| 3499 | if (Pred == ICmpInst::ICMP_SGT && C->isMaxSignedValue()) |
| 3500 | return FalseVal; |
| 3501 | // X > MAX ? T : F --> F |
| 3502 | if (Pred == ICmpInst::ICMP_UGT && C->isMaxValue()) |
| 3503 | return FalseVal; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | // If we have an equality comparison, then we know the value in one of the |
| 3508 | // arms of the select. See if substituting this value into the arm and |
| 3509 | // simplifying the result yields the same value as the other arm. |
| 3510 | if (Pred == ICmpInst::ICMP_EQ) { |
| 3511 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3512 | TrueVal || |
| 3513 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3514 | TrueVal) |
| 3515 | return FalseVal; |
| 3516 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3517 | FalseVal || |
| 3518 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3519 | FalseVal) |
| 3520 | return FalseVal; |
| 3521 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 3522 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3523 | FalseVal || |
| 3524 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3525 | FalseVal) |
| 3526 | return TrueVal; |
| 3527 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3528 | TrueVal || |
| 3529 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3530 | TrueVal) |
| 3531 | return TrueVal; |
| 3532 | } |
| 3533 | |
| 3534 | return nullptr; |
| 3535 | } |
| 3536 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3537 | /// Given operands for a SelectInst, see if we can fold the result. |
| 3538 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3539 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 3540 | Value *FalseVal, const Query &Q, |
| 3541 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3542 | // select true, X, Y -> X |
| 3543 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3544 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 3545 | if (CB->isAllOnesValue()) |
| 3546 | return TrueVal; |
| 3547 | if (CB->isNullValue()) |
| 3548 | return FalseVal; |
| 3549 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3550 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3551 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3552 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3553 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3554 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3555 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3556 | if (isa<Constant>(TrueVal)) |
| 3557 | return TrueVal; |
| 3558 | return FalseVal; |
| 3559 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3560 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3561 | return FalseVal; |
| 3562 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3563 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3564 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3565 | if (Value *V = |
| 3566 | simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse)) |
| 3567 | return V; |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3568 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3569 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3572 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3573 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3574 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3575 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3576 | const Instruction *CxtI) { |
| 3577 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3578 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3581 | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
| 3582 | /// If not, this returns null. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3583 | static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3584 | const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3585 | // The type of the GEP pointer operand. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3586 | unsigned AS = |
| 3587 | cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3588 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3589 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3590 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3591 | return Ops[0]; |
| 3592 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3593 | // Compute the (pointer) type returned by the GEP instruction. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3594 | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1)); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3595 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3596 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3597 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
| 3598 | |
| 3599 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3600 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3601 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3602 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3603 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3604 | if (match(Ops[1], m_Zero())) |
| 3605 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3606 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3607 | Type *Ty = SrcTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3608 | if (Ty->isSized()) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3609 | Value *P; |
| 3610 | uint64_t C; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3611 | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3612 | // getelementptr P, N -> P if P points to a type of zero size. |
| 3613 | if (TyAllocSize == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3614 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3615 | |
| 3616 | // The following transforms are only safe if the ptrtoint cast |
| 3617 | // doesn't truncate the pointers. |
| 3618 | if (Ops[1]->getType()->getScalarSizeInBits() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3619 | Q.DL.getPointerSizeInBits(AS)) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3620 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3621 | if (match(P, m_Zero())) |
| 3622 | return Constant::getNullValue(GEPTy); |
| 3623 | Value *Temp; |
| 3624 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3625 | if (Temp->getType() == GEPTy) |
| 3626 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3627 | return nullptr; |
| 3628 | }; |
| 3629 | |
| 3630 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3631 | if (TyAllocSize == 1 && |
| 3632 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3633 | if (Value *R = PtrToIntOrZero(P)) |
| 3634 | return R; |
| 3635 | |
| 3636 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3637 | // if P points to a type of size 1 << C. |
| 3638 | if (match(Ops[1], |
| 3639 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3640 | m_ConstantInt(C))) && |
| 3641 | TyAllocSize == 1ULL << C) |
| 3642 | if (Value *R = PtrToIntOrZero(P)) |
| 3643 | return R; |
| 3644 | |
| 3645 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3646 | // if P points to a type of size C. |
| 3647 | if (match(Ops[1], |
| 3648 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3649 | m_SpecificInt(TyAllocSize)))) |
| 3650 | if (Value *R = PtrToIntOrZero(P)) |
| 3651 | return R; |
| 3652 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3653 | } |
| 3654 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3655 | |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3656 | if (Q.DL.getTypeAllocSize(LastType) == 1 && |
| 3657 | all_of(Ops.slice(1).drop_back(1), |
| 3658 | [](Value *Idx) { return match(Idx, m_Zero()); })) { |
| 3659 | unsigned PtrWidth = |
| 3660 | Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); |
| 3661 | if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) { |
| 3662 | APInt BasePtrOffset(PtrWidth, 0); |
| 3663 | Value *StrippedBasePtr = |
| 3664 | Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, |
| 3665 | BasePtrOffset); |
| 3666 | |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3667 | // gep (gep V, C), (sub 0, V) -> C |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3668 | if (match(Ops.back(), |
| 3669 | m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { |
| 3670 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
| 3671 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3672 | } |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3673 | // gep (gep V, C), (xor V, -1) -> C-1 |
| 3674 | if (match(Ops.back(), |
| 3675 | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) { |
| 3676 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
| 3677 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3678 | } |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3682 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3683 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3684 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3685 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3686 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3687 | return ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]), |
| 3688 | Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3691 | Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3692 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3693 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3694 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3695 | const Instruction *CxtI) { |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3696 | return ::SimplifyGEPInst(SrcTy, Ops, |
| 3697 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3700 | /// Given operands for an InsertValueInst, see if we can fold the result. |
| 3701 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3702 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3703 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 3704 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3705 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3706 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3707 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3708 | |
| 3709 | // insertvalue x, undef, n -> x |
| 3710 | if (match(Val, m_Undef())) |
| 3711 | return Agg; |
| 3712 | |
| 3713 | // insertvalue x, (extractvalue y, n), n |
| 3714 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3715 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3716 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3717 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3718 | if (match(Agg, m_Undef())) |
| 3719 | return EV->getAggregateOperand(); |
| 3720 | |
| 3721 | // insertvalue y, (extractvalue y, n), n -> y |
| 3722 | if (Agg == EV->getAggregateOperand()) |
| 3723 | return Agg; |
| 3724 | } |
| 3725 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3726 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3729 | Value *llvm::SimplifyInsertValueInst( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3730 | Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3731 | const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, |
| 3732 | const Instruction *CxtI) { |
| 3733 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3734 | RecursionLimit); |
| 3735 | } |
| 3736 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3737 | /// Given operands for an ExtractValueInst, see if we can fold the result. |
| 3738 | /// If not, this returns null. |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3739 | static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3740 | const Query &, unsigned) { |
| 3741 | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
| 3742 | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
| 3743 | |
| 3744 | // extractvalue x, (insertvalue y, elt, n), n -> elt |
| 3745 | unsigned NumIdxs = Idxs.size(); |
| 3746 | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
| 3747 | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) { |
| 3748 | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
| 3749 | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
| 3750 | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
| 3751 | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
| 3752 | Idxs.slice(0, NumCommonIdxs)) { |
| 3753 | if (NumIdxs == NumInsertValueIdxs) |
| 3754 | return IVI->getInsertedValueOperand(); |
| 3755 | break; |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | return nullptr; |
| 3760 | } |
| 3761 | |
| 3762 | Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3763 | const DataLayout &DL, |
| 3764 | const TargetLibraryInfo *TLI, |
| 3765 | const DominatorTree *DT, |
| 3766 | AssumptionCache *AC, |
| 3767 | const Instruction *CxtI) { |
| 3768 | return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI), |
| 3769 | RecursionLimit); |
| 3770 | } |
| 3771 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3772 | /// Given operands for an ExtractElementInst, see if we can fold the result. |
| 3773 | /// If not, this returns null. |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3774 | static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &, |
| 3775 | unsigned) { |
| 3776 | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
| 3777 | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
| 3778 | return ConstantFoldExtractElementInstruction(CVec, CIdx); |
| 3779 | |
| 3780 | // The index is not relevant if our vector is a splat. |
| 3781 | if (auto *Splat = CVec->getSplatValue()) |
| 3782 | return Splat; |
| 3783 | |
| 3784 | if (isa<UndefValue>(Vec)) |
| 3785 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 3786 | } |
| 3787 | |
| 3788 | // If extracting a specified index from the vector, see if we can recursively |
| 3789 | // find a previously computed scalar that was inserted into the vector. |
David Majnemer | 8e335ca | 2015-08-18 22:18:22 +0000 | [diff] [blame] | 3790 | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) |
| 3791 | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3792 | return Elt; |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3793 | |
| 3794 | return nullptr; |
| 3795 | } |
| 3796 | |
| 3797 | Value *llvm::SimplifyExtractElementInst( |
| 3798 | Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 3799 | const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) { |
| 3800 | return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI), |
| 3801 | RecursionLimit); |
| 3802 | } |
| 3803 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3804 | /// See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3805 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3806 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 3807 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3808 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3809 | bool HasUndefInput = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3810 | for (Value *Incoming : PN->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3811 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 3812 | if (Incoming == PN) continue; |
| 3813 | if (isa<UndefValue>(Incoming)) { |
| 3814 | // Remember that we saw an undef value, but otherwise ignore them. |
| 3815 | HasUndefInput = true; |
| 3816 | continue; |
| 3817 | } |
| 3818 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3819 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3820 | CommonValue = Incoming; |
| 3821 | } |
| 3822 | |
| 3823 | // If CommonValue is null then all of the incoming values were either undef or |
| 3824 | // equal to the phi node itself. |
| 3825 | if (!CommonValue) |
| 3826 | return UndefValue::get(PN->getType()); |
| 3827 | |
| 3828 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 3829 | // instruction, we cannot return X as the result of the PHI node unless it |
| 3830 | // dominates the PHI block. |
| 3831 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3832 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 3833 | |
| 3834 | return CommonValue; |
| 3835 | } |
| 3836 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 3837 | static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, |
| 3838 | Type *Ty, const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 126de5d | 2016-07-25 03:39:21 +0000 | [diff] [blame] | 3839 | if (auto *C = dyn_cast<Constant>(Op)) |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 3840 | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 3841 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 3842 | if (auto *CI = dyn_cast<CastInst>(Op)) { |
| 3843 | auto *Src = CI->getOperand(0); |
| 3844 | Type *SrcTy = Src->getType(); |
| 3845 | Type *MidTy = CI->getType(); |
| 3846 | Type *DstTy = Ty; |
| 3847 | if (Src->getType() == Ty) { |
| 3848 | auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode()); |
| 3849 | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
| 3850 | Type *SrcIntPtrTy = |
| 3851 | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr; |
| 3852 | Type *MidIntPtrTy = |
| 3853 | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr; |
| 3854 | Type *DstIntPtrTy = |
| 3855 | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr; |
| 3856 | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
| 3857 | SrcIntPtrTy, MidIntPtrTy, |
| 3858 | DstIntPtrTy) == Instruction::BitCast) |
| 3859 | return Src; |
| 3860 | } |
| 3861 | } |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 3862 | |
| 3863 | // bitcast x -> x |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 3864 | if (CastOpc == Instruction::BitCast) |
| 3865 | if (Op->getType() == Ty) |
| 3866 | return Op; |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 3867 | |
| 3868 | return nullptr; |
| 3869 | } |
| 3870 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 3871 | Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
| 3872 | const DataLayout &DL, |
| 3873 | const TargetLibraryInfo *TLI, |
| 3874 | const DominatorTree *DT, AssumptionCache *AC, |
| 3875 | const Instruction *CxtI) { |
| 3876 | return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI), |
| 3877 | RecursionLimit); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3880 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3881 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3882 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 3883 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3884 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3885 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3886 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3887 | case Instruction::Add: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3888 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3889 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3890 | case Instruction::FAdd: |
| 3891 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 3892 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3893 | case Instruction::Sub: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3894 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3895 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3896 | case Instruction::FSub: |
| 3897 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 3898 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3899 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 3900 | case Instruction::FMul: |
| 3901 | return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3902 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 3903 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 3904 | case Instruction::FDiv: |
| 3905 | return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3906 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 3907 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 3908 | case Instruction::FRem: |
| 3909 | return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3910 | case Instruction::Shl: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 3911 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3912 | Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3913 | case Instruction::LShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3914 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 3915 | case Instruction::AShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3916 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
| 3917 | case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 3918 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, Q, MaxRecurse); |
| 3919 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3920 | default: |
| 3921 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 3922 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 3923 | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 3924 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 3925 | // If the operation is associative, try some generic simplifications. |
| 3926 | if (Instruction::isAssociative(Opcode)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3927 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 3928 | return V; |
| 3929 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3930 | // 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] | 3931 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3932 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3933 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3934 | return V; |
| 3935 | |
| 3936 | // If the operation is with the result of a phi instruction, check whether |
| 3937 | // 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] | 3938 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3939 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 3940 | return V; |
| 3941 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3942 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3943 | } |
| 3944 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3945 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3946 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 3947 | /// If not, this returns null. |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 3948 | /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the |
| 3949 | /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp. |
| 3950 | static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
| 3951 | const FastMathFlags &FMF, const Query &Q, |
| 3952 | unsigned MaxRecurse) { |
| 3953 | switch (Opcode) { |
| 3954 | case Instruction::FAdd: |
| 3955 | return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 3956 | case Instruction::FSub: |
| 3957 | return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 3958 | case Instruction::FMul: |
| 3959 | return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 3960 | default: |
| 3961 | return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
| 3962 | } |
| 3963 | } |
| 3964 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3965 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3966 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3967 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3968 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3969 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3970 | RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 3973 | Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3974 | const FastMathFlags &FMF, const DataLayout &DL, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 3975 | const TargetLibraryInfo *TLI, |
| 3976 | const DominatorTree *DT, AssumptionCache *AC, |
| 3977 | const Instruction *CxtI) { |
| 3978 | return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI), |
| 3979 | RecursionLimit); |
| 3980 | } |
| 3981 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3982 | /// Given operands for a CmpInst, see if we can fold the result. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3983 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3984 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3985 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3986 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3987 | return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3991 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3992 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3993 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3994 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3995 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3996 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 3997 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 3998 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 3999 | switch (ID) { |
| 4000 | default: return false; |
| 4001 | |
| 4002 | // Unary idempotent: f(f(x)) = f(x) |
| 4003 | case Intrinsic::fabs: |
| 4004 | case Intrinsic::floor: |
| 4005 | case Intrinsic::ceil: |
| 4006 | case Intrinsic::trunc: |
| 4007 | case Intrinsic::rint: |
| 4008 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4009 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4010 | return true; |
| 4011 | } |
| 4012 | } |
| 4013 | |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4014 | static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
| 4015 | const DataLayout &DL) { |
| 4016 | GlobalValue *PtrSym; |
| 4017 | APInt PtrOffset; |
| 4018 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
| 4019 | return nullptr; |
| 4020 | |
| 4021 | Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext()); |
| 4022 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
| 4023 | Type *Int32PtrTy = Int32Ty->getPointerTo(); |
| 4024 | Type *Int64Ty = Type::getInt64Ty(Ptr->getContext()); |
| 4025 | |
| 4026 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
| 4027 | if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64) |
| 4028 | return nullptr; |
| 4029 | |
| 4030 | uint64_t OffsetInt = OffsetConstInt->getSExtValue(); |
| 4031 | if (OffsetInt % 4 != 0) |
| 4032 | return nullptr; |
| 4033 | |
| 4034 | Constant *C = ConstantExpr::getGetElementPtr( |
| 4035 | Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy), |
| 4036 | ConstantInt::get(Int64Ty, OffsetInt / 4)); |
| 4037 | Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL); |
| 4038 | if (!Loaded) |
| 4039 | return nullptr; |
| 4040 | |
| 4041 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
| 4042 | if (!LoadedCE) |
| 4043 | return nullptr; |
| 4044 | |
| 4045 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
| 4046 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4047 | if (!LoadedCE) |
| 4048 | return nullptr; |
| 4049 | } |
| 4050 | |
| 4051 | if (LoadedCE->getOpcode() != Instruction::Sub) |
| 4052 | return nullptr; |
| 4053 | |
| 4054 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4055 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
| 4056 | return nullptr; |
| 4057 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
| 4058 | |
| 4059 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
| 4060 | GlobalValue *LoadedRHSSym; |
| 4061 | APInt LoadedRHSOffset; |
| 4062 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
| 4063 | DL) || |
| 4064 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
| 4065 | return nullptr; |
| 4066 | |
| 4067 | return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy); |
| 4068 | } |
| 4069 | |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4070 | static bool maskIsAllZeroOrUndef(Value *Mask) { |
| 4071 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 4072 | if (!ConstMask) |
| 4073 | return false; |
| 4074 | if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask)) |
| 4075 | return true; |
| 4076 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 4077 | ++I) { |
| 4078 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 4079 | if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt)) |
| 4080 | continue; |
| 4081 | return false; |
| 4082 | } |
| 4083 | return true; |
| 4084 | } |
| 4085 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4086 | template <typename IterTy> |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4087 | static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4088 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4089 | Intrinsic::ID IID = F->getIntrinsicID(); |
| 4090 | unsigned NumOperands = std::distance(ArgBegin, ArgEnd); |
| 4091 | Type *ReturnType = F->getReturnType(); |
| 4092 | |
| 4093 | // Binary Ops |
| 4094 | if (NumOperands == 2) { |
| 4095 | Value *LHS = *ArgBegin; |
| 4096 | Value *RHS = *(ArgBegin + 1); |
| 4097 | if (IID == Intrinsic::usub_with_overflow || |
| 4098 | IID == Intrinsic::ssub_with_overflow) { |
| 4099 | // X - X -> { 0, false } |
| 4100 | if (LHS == RHS) |
| 4101 | return Constant::getNullValue(ReturnType); |
| 4102 | |
| 4103 | // X - undef -> undef |
| 4104 | // undef - X -> undef |
| 4105 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
| 4106 | return UndefValue::get(ReturnType); |
| 4107 | } |
| 4108 | |
| 4109 | if (IID == Intrinsic::uadd_with_overflow || |
| 4110 | IID == Intrinsic::sadd_with_overflow) { |
| 4111 | // X + undef -> undef |
| 4112 | if (isa<UndefValue>(RHS)) |
| 4113 | return UndefValue::get(ReturnType); |
| 4114 | } |
| 4115 | |
| 4116 | if (IID == Intrinsic::umul_with_overflow || |
| 4117 | IID == Intrinsic::smul_with_overflow) { |
| 4118 | // X * 0 -> { 0, false } |
| 4119 | if (match(RHS, m_Zero())) |
| 4120 | return Constant::getNullValue(ReturnType); |
| 4121 | |
| 4122 | // X * undef -> { 0, false } |
| 4123 | if (match(RHS, m_Undef())) |
| 4124 | return Constant::getNullValue(ReturnType); |
| 4125 | } |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4126 | |
| 4127 | if (IID == Intrinsic::load_relative && isa<Constant>(LHS) && |
| 4128 | isa<Constant>(RHS)) |
| 4129 | return SimplifyRelativeLoad(cast<Constant>(LHS), cast<Constant>(RHS), |
| 4130 | Q.DL); |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
David Majnemer | d77a3b6 | 2016-07-13 23:32:53 +0000 | [diff] [blame] | 4133 | // Simplify calls to llvm.masked.load.* |
| 4134 | if (IID == Intrinsic::masked_load) { |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4135 | Value *MaskArg = ArgBegin[2]; |
| 4136 | Value *PassthruArg = ArgBegin[3]; |
| 4137 | // If the mask is all zeros or undef, the "passthru" argument is the result. |
| 4138 | if (maskIsAllZeroOrUndef(MaskArg)) |
| 4139 | return PassthruArg; |
David Majnemer | d77a3b6 | 2016-07-13 23:32:53 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4142 | // Perform idempotent optimizations |
| 4143 | if (!IsIdempotent(IID)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4144 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4145 | |
| 4146 | // Unary Ops |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4147 | if (NumOperands == 1) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4148 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) |
| 4149 | if (II->getIntrinsicID() == IID) |
| 4150 | return II; |
| 4151 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4152 | return nullptr; |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4155 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4156 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4157 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4158 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4159 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 4160 | Ty = PTy->getElementType(); |
| 4161 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 4162 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4163 | // call undef -> undef |
David Majnemer | bb53d23 | 2016-06-25 07:37:30 +0000 | [diff] [blame] | 4164 | // call null -> undef |
| 4165 | if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4166 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4167 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4168 | Function *F = dyn_cast<Function>(V); |
| 4169 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4170 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4171 | |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4172 | if (F->isIntrinsic()) |
| 4173 | if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse)) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4174 | return Ret; |
| 4175 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4176 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4177 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4178 | |
| 4179 | SmallVector<Constant *, 4> ConstantArgs; |
| 4180 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 4181 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 4182 | Constant *C = dyn_cast<Constant>(*I); |
| 4183 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4184 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4185 | ConstantArgs.push_back(C); |
| 4186 | } |
| 4187 | |
| 4188 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4191 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4192 | User::op_iterator ArgEnd, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4193 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
| 4194 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4195 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4196 | RecursionLimit); |
| 4197 | } |
| 4198 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4199 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4200 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4201 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4202 | const Instruction *CxtI) { |
| 4203 | return ::SimplifyCall(V, Args.begin(), Args.end(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4204 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4207 | /// See if we can compute a simplified version of this instruction. |
| 4208 | /// If not, this returns null. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4209 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 4210 | const TargetLibraryInfo *TLI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4211 | const DominatorTree *DT, AssumptionCache *AC) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4212 | Value *Result; |
| 4213 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4214 | switch (I->getOpcode()) { |
| 4215 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 4216 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4217 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4218 | case Instruction::FAdd: |
| 4219 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4220 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4221 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 4222 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4223 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 4224 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4225 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
| 4226 | TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4227 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4228 | case Instruction::FSub: |
| 4229 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4230 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4231 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4232 | case Instruction::Sub: |
| 4233 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 4234 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4235 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
| 4236 | TLI, DT, AC, I); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4237 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4238 | case Instruction::FMul: |
| 4239 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4240 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4241 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4242 | case Instruction::Mul: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4243 | Result = |
| 4244 | SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4245 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4246 | case Instruction::SDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4247 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
| 4248 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4249 | break; |
| 4250 | case Instruction::UDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4251 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
| 4252 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4253 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4254 | case Instruction::FDiv: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4255 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
| 4256 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4257 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4258 | case Instruction::SRem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4259 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
| 4260 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4261 | break; |
| 4262 | case Instruction::URem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4263 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
| 4264 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4265 | break; |
| 4266 | case Instruction::FRem: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4267 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
| 4268 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4269 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4270 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4271 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 4272 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4273 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
| 4274 | TLI, DT, AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4275 | break; |
| 4276 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4277 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4278 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
| 4279 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4280 | break; |
| 4281 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4282 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4283 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
| 4284 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4285 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4286 | case Instruction::And: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4287 | Result = |
| 4288 | SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4289 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4290 | case Instruction::Or: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4291 | Result = |
| 4292 | SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4293 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4294 | case Instruction::Xor: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4295 | Result = |
| 4296 | SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4297 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4298 | case Instruction::ICmp: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4299 | Result = |
| 4300 | SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), I->getOperand(0), |
| 4301 | I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4302 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4303 | case Instruction::FCmp: |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4304 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 4305 | I->getOperand(0), I->getOperand(1), |
| 4306 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4307 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 4308 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4309 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4310 | I->getOperand(2), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4311 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4312 | case Instruction::GetElementPtr: { |
| 4313 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 4314 | Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(), |
| 4315 | Ops, DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4316 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4317 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4318 | case Instruction::InsertValue: { |
| 4319 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 4320 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 4321 | IV->getInsertedValueOperand(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4322 | IV->getIndices(), DL, TLI, DT, AC, I); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4323 | break; |
| 4324 | } |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4325 | case Instruction::ExtractValue: { |
| 4326 | auto *EVI = cast<ExtractValueInst>(I); |
| 4327 | Result = SimplifyExtractValueInst(EVI->getAggregateOperand(), |
| 4328 | EVI->getIndices(), DL, TLI, DT, AC, I); |
| 4329 | break; |
| 4330 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4331 | case Instruction::ExtractElement: { |
| 4332 | auto *EEI = cast<ExtractElementInst>(I); |
| 4333 | Result = SimplifyExtractElementInst( |
| 4334 | EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I); |
| 4335 | break; |
| 4336 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 4337 | case Instruction::PHI: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4338 | Result = SimplifyPHINode(cast<PHINode>(I), Query(DL, TLI, DT, AC, I)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4339 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4340 | case Instruction::Call: { |
| 4341 | CallSite CS(cast<CallInst>(I)); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4342 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL, |
| 4343 | TLI, DT, AC, I); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4344 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4345 | } |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4346 | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
| 4347 | #include "llvm/IR/Instruction.def" |
| 4348 | #undef HANDLE_CAST_INST |
| 4349 | Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(), |
| 4350 | DL, TLI, DT, AC, I); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4351 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4352 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4353 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4354 | // In general, it is possible for computeKnownBits to determine all bits in a |
| 4355 | // value even when the operands are not all constants. |
| 4356 | if (!Result && I->getType()->isIntegerTy()) { |
| 4357 | unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
| 4358 | APInt KnownZero(BitWidth, 0); |
| 4359 | APInt KnownOne(BitWidth, 0); |
| 4360 | computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT); |
| 4361 | if ((KnownZero | KnownOne).isAllOnesValue()) |
| 4362 | Result = ConstantInt::get(I->getContext(), KnownOne); |
| 4363 | } |
| 4364 | |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4365 | /// If called on unreachable code, the above logic may report that the |
| 4366 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 4367 | /// detecting that case here, returning a safe value instead. |
| 4368 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
Sanjay Patel | f44bd38 | 2016-01-20 18:59:48 +0000 | [diff] [blame] | 4371 | /// \brief Implementation of recursive simplification through an instruction's |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4372 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4373 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4374 | /// This is the common implementation of the recursive simplification routines. |
| 4375 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 4376 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 4377 | /// instructions to process and attempt to simplify it using |
| 4378 | /// InstructionSimplify. |
| 4379 | /// |
| 4380 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 4381 | /// in simplified value does not count toward this. |
| 4382 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4383 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4384 | const DominatorTree *DT, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4385 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4386 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4387 | SmallSetVector<Instruction *, 8> Worklist; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4388 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4389 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4390 | // If we have an explicit value to collapse to, do that round of the |
| 4391 | // simplification loop by hand initially. |
| 4392 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4393 | for (User *U : I->users()) |
| 4394 | if (U != I) |
| 4395 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4396 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4397 | // Replace the instruction with its simplified value. |
| 4398 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 4399 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4400 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4401 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4402 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4403 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4404 | I->eraseFromParent(); |
| 4405 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4406 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4407 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4408 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4409 | // Note that we must test the size on each iteration, the worklist can grow. |
| 4410 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 4411 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4412 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4413 | // See if this instruction simplifies. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4414 | SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4415 | if (!SimpleV) |
| 4416 | continue; |
| 4417 | |
| 4418 | Simplified = true; |
| 4419 | |
| 4420 | // Stash away all the uses of the old instruction so we can check them for |
| 4421 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 4422 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4423 | for (User *U : I->users()) |
| 4424 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Replace the instruction with its simplified value. |
| 4427 | I->replaceAllUsesWith(SimpleV); |
| 4428 | |
| 4429 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4430 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4431 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4432 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4433 | I->eraseFromParent(); |
| 4434 | } |
| 4435 | return Simplified; |
| 4436 | } |
| 4437 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4438 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4439 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4440 | const DominatorTree *DT, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4441 | AssumptionCache *AC) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4442 | return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4446 | const TargetLibraryInfo *TLI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4447 | const DominatorTree *DT, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4448 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4449 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 4450 | assert(SimpleV && "Must provide a simplified value."); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4451 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4452 | } |