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" |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 33 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/GlobalAlias.h" |
| 35 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 36 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 37 | #include "llvm/IR/ValueHandle.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 40 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 41 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "instsimplify" |
| 43 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 44 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 45 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 46 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 47 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 48 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 49 | namespace { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 50 | struct Query { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 51 | const DataLayout &DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 52 | const TargetLibraryInfo *TLI; |
| 53 | const DominatorTree *DT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 54 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 55 | const Instruction *CxtI; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 56 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 57 | Query(const DataLayout &DL, const TargetLibraryInfo *tli, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 58 | const DominatorTree *dt, AssumptionCache *ac = nullptr, |
| 59 | const Instruction *cxti = nullptr) |
| 60 | : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 61 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 62 | } // end anonymous namespace |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 63 | |
| 64 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 65 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 66 | unsigned); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 67 | static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
| 68 | const Query &, unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 69 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 70 | unsigned); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 71 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 72 | const Query &Q, unsigned MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 73 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 74 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 75 | static Value *SimplifyCastInst(unsigned, Value *, Type *, |
| 76 | const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 77 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 78 | /// 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] | 79 | /// a vector with every element false, as appropriate for the type. |
| 80 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 81 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 82 | "Expected i1 type or a vector of i1!"); |
| 83 | return Constant::getNullValue(Ty); |
| 84 | } |
| 85 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 86 | /// 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] | 87 | /// a vector with every element true, as appropriate for the type. |
| 88 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 89 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 90 | "Expected i1 type or a vector of i1!"); |
| 91 | return Constant::getAllOnesValue(Ty); |
| 92 | } |
| 93 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 94 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 95 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 96 | Value *RHS) { |
| 97 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 98 | if (!Cmp) |
| 99 | return false; |
| 100 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 101 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 102 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 103 | return true; |
| 104 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 105 | CRHS == LHS; |
| 106 | } |
| 107 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 108 | /// Does the given value dominate the specified phi node? |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 109 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 110 | Instruction *I = dyn_cast<Instruction>(V); |
| 111 | if (!I) |
| 112 | // Arguments and constants dominate all instructions. |
| 113 | return true; |
| 114 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 115 | // If we are processing instructions (and/or basic blocks) that have not been |
| 116 | // fully added to a function, the parent nodes may still be null. Simply |
| 117 | // return the conservative answer in these cases. |
| 118 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 119 | return false; |
| 120 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 121 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 122 | if (DT) { |
| 123 | if (!DT->isReachableFromEntry(P->getParent())) |
| 124 | return true; |
| 125 | if (!DT->isReachableFromEntry(I->getParent())) |
| 126 | return false; |
| 127 | return DT->dominates(I, P); |
| 128 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 129 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 130 | // Otherwise, if the instruction is in the entry block and is not an invoke, |
| 131 | // then it obviously dominates all phi nodes. |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 132 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 133 | !isa<InvokeInst>(I)) |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 134 | return true; |
| 135 | |
| 136 | return false; |
| 137 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 138 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 139 | /// Simplify "A op (B op' C)" by distributing op over op', turning it into |
| 140 | /// "(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] | 141 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 142 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 143 | /// Returns the simplified value, or null if no simplification was performed. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 144 | static Value *ExpandBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, |
| 145 | Instruction::BinaryOps OpcodeToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 146 | unsigned MaxRecurse) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 147 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 148 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 149 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 150 | |
| 151 | // Check whether the expression has the form "(A op' B) op C". |
| 152 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 153 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 154 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 155 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 156 | // Do "A op C" and "B op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 157 | if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) |
| 158 | if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 159 | // They do! Return "L op' R" if it simplifies or is already available. |
| 160 | // 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] | 161 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 162 | && L == B && R == A)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 163 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 164 | return LHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 165 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 166 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 167 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 168 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 169 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 170 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | // Check whether the expression has the form "A op (B op' C)". |
| 175 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 176 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 177 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 178 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 179 | // Do "A op B" and "A op C" both simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 180 | if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) |
| 181 | if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) { |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 182 | // They do! Return "L op' R" if it simplifies or is already available. |
| 183 | // 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] | 184 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 185 | && L == C && R == B)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 186 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 187 | return RHS; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 188 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 189 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 190 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 191 | ++NumExpand; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 192 | return V; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 193 | } |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 197 | return nullptr; |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 200 | /// Generic simplifications for associative binary operations. |
| 201 | /// Returns the simpler value, or null if none was found. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 202 | static Value *SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode, |
| 203 | Value *LHS, Value *RHS, const Query &Q, |
| 204 | unsigned MaxRecurse) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 205 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 206 | |
| 207 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 208 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 209 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 210 | |
| 211 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 212 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 213 | |
| 214 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 215 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 216 | Value *A = Op0->getOperand(0); |
| 217 | Value *B = Op0->getOperand(1); |
| 218 | Value *C = RHS; |
| 219 | |
| 220 | // Does "B op C" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 221 | if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 222 | // It does! Return "A op V" if it simplifies or is already available. |
| 223 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 224 | if (V == B) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 225 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 226 | if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 227 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 228 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 229 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 234 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 235 | Value *A = LHS; |
| 236 | Value *B = Op1->getOperand(0); |
| 237 | Value *C = Op1->getOperand(1); |
| 238 | |
| 239 | // Does "A op B" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 240 | if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 241 | // It does! Return "V op C" if it simplifies or is already available. |
| 242 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 243 | if (V == B) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 244 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 245 | if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 246 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 247 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 248 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // The remaining transforms require commutativity as well as associativity. |
| 253 | if (!Instruction::isCommutative(Opcode)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 254 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 255 | |
| 256 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 257 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 258 | Value *A = Op0->getOperand(0); |
| 259 | Value *B = Op0->getOperand(1); |
| 260 | Value *C = RHS; |
| 261 | |
| 262 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 263 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 264 | // It does! Return "V op B" if it simplifies or is already available. |
| 265 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 266 | if (V == A) return LHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 267 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 268 | if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 269 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 270 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 271 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 276 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 277 | Value *A = LHS; |
| 278 | Value *B = Op1->getOperand(0); |
| 279 | Value *C = Op1->getOperand(1); |
| 280 | |
| 281 | // Does "C op A" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 282 | if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 283 | // It does! Return "B op V" if it simplifies or is already available. |
| 284 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 285 | if (V == C) return RHS; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 286 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 287 | if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 288 | ++NumReassoc; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 289 | return W; |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 290 | } |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 294 | return nullptr; |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 297 | /// In the case of a binary operation with a select instruction as an operand, |
| 298 | /// try to simplify the binop by seeing whether evaluating it on both branches |
| 299 | /// of the select results in the same value. Returns the common value if so, |
| 300 | /// otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 301 | static Value *ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, |
| 302 | Value *RHS, const Query &Q, |
| 303 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 304 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 305 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 306 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 307 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 308 | SelectInst *SI; |
| 309 | if (isa<SelectInst>(LHS)) { |
| 310 | SI = cast<SelectInst>(LHS); |
| 311 | } else { |
| 312 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 313 | SI = cast<SelectInst>(RHS); |
| 314 | } |
| 315 | |
| 316 | // Evaluate the BinOp on the true and false branches of the select. |
| 317 | Value *TV; |
| 318 | Value *FV; |
| 319 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 320 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 321 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 322 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 323 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 324 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 327 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 328 | // If they both failed to simplify then return null. |
| 329 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 330 | return TV; |
| 331 | |
| 332 | // If one branch simplified to undef, return the other one. |
| 333 | if (TV && isa<UndefValue>(TV)) |
| 334 | return FV; |
| 335 | if (FV && isa<UndefValue>(FV)) |
| 336 | return TV; |
| 337 | |
| 338 | // If applying the operation did not change the true and false select values, |
| 339 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 340 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 341 | return SI; |
| 342 | |
| 343 | // If one branch simplified and the other did not, and the simplified |
| 344 | // value is equal to the unsimplified one, return the simplified value. |
| 345 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 346 | if ((FV && !TV) || (TV && !FV)) { |
| 347 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 348 | // same as the original operation. |
| 349 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 350 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 351 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 352 | // We already know that "op" is the same as for the simplified value. See |
| 353 | // if the operands match too. If so, return the simplified value. |
| 354 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 355 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 356 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 357 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 358 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 359 | return Simplified; |
| 360 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 361 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 362 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 363 | return Simplified; |
| 364 | } |
| 365 | } |
| 366 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 367 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 370 | /// In the case of a comparison with a select instruction, try to simplify the |
| 371 | /// comparison by seeing whether both branches of the select result in the same |
| 372 | /// value. Returns the common value if so, otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 373 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 374 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 375 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 376 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 377 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 378 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 379 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 380 | // Make sure the select is on the LHS. |
| 381 | if (!isa<SelectInst>(LHS)) { |
| 382 | std::swap(LHS, RHS); |
| 383 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 384 | } |
| 385 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 386 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 387 | Value *Cond = SI->getCondition(); |
| 388 | Value *TV = SI->getTrueValue(); |
| 389 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 390 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 391 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 392 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 393 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 394 | if (TCmp == Cond) { |
| 395 | // It not only simplified, it simplified to the select condition. Replace |
| 396 | // it with 'true'. |
| 397 | TCmp = getTrue(Cond->getType()); |
| 398 | } else if (!TCmp) { |
| 399 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 400 | // condition then we can replace it with 'true'. Otherwise give up. |
| 401 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 402 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 403 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 406 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 407 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 408 | if (FCmp == Cond) { |
| 409 | // It not only simplified, it simplified to the select condition. Replace |
| 410 | // it with 'false'. |
| 411 | FCmp = getFalse(Cond->getType()); |
| 412 | } else if (!FCmp) { |
| 413 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 414 | // condition then we can replace it with 'false'. Otherwise give up. |
| 415 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 416 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 417 | FCmp = getFalse(Cond->getType()); |
| 418 | } |
| 419 | |
| 420 | // If both sides simplified to the same value, then use it as the result of |
| 421 | // the original comparison. |
| 422 | if (TCmp == FCmp) |
| 423 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 424 | |
| 425 | // The remaining cases only make sense if the select condition has the same |
| 426 | // type as the result of the comparison, so bail out if this is not so. |
| 427 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 428 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 429 | // If the false value simplified to false, then the result of the compare |
| 430 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 431 | // value simplified to false and the true value to true, returning "Cond". |
| 432 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 433 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 434 | return V; |
| 435 | // If the true value simplified to true, then the result of the compare |
| 436 | // is equal to "Cond || FCmp". |
| 437 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 438 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 439 | return V; |
| 440 | // Finally, if the false value simplified to true and the true value to |
| 441 | // false, then the result of the compare is equal to "!Cond". |
| 442 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 443 | if (Value *V = |
| 444 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 445 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 446 | return V; |
| 447 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 448 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 451 | /// In the case of a binary operation with an operand that is a PHI instruction, |
| 452 | /// try to simplify the binop by seeing whether evaluating it on the incoming |
| 453 | /// phi values yields the same result for every value. If so returns the common |
| 454 | /// value, otherwise returns null. |
Craig Topper | 60dd9cd | 2017-04-07 05:57:51 +0000 | [diff] [blame] | 455 | static Value *ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, |
| 456 | Value *RHS, const Query &Q, |
| 457 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 458 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 459 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 460 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 461 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 462 | PHINode *PI; |
| 463 | if (isa<PHINode>(LHS)) { |
| 464 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 465 | // 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] | 466 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 467 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 468 | } else { |
| 469 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 470 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 471 | // 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] | 472 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 473 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 477 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 478 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 479 | // 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] | 480 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 481 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 482 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 483 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 484 | // If the operation failed to simplify, or simplified to a different value |
| 485 | // to previously, then give up. |
| 486 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 487 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 488 | CommonValue = V; |
| 489 | } |
| 490 | |
| 491 | return CommonValue; |
| 492 | } |
| 493 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 494 | /// In the case of a comparison with a PHI instruction, try to simplify the |
| 495 | /// comparison by seeing whether comparing with all of the incoming phi values |
| 496 | /// yields the same result every time. If so returns the common result, |
| 497 | /// otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 498 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 499 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 500 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 501 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 502 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 503 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 504 | // Make sure the phi is on the LHS. |
| 505 | if (!isa<PHINode>(LHS)) { |
| 506 | std::swap(LHS, RHS); |
| 507 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 508 | } |
| 509 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 510 | PHINode *PI = cast<PHINode>(LHS); |
| 511 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 512 | // 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] | 513 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 514 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 515 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 516 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 517 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 518 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 519 | // 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] | 520 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 521 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 522 | // If the operation failed to simplify, or simplified to a different value |
| 523 | // to previously, then give up. |
| 524 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 525 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 526 | CommonValue = V; |
| 527 | } |
| 528 | |
| 529 | return CommonValue; |
| 530 | } |
| 531 | |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 532 | static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode, |
| 533 | Value *&Op0, Value *&Op1, |
| 534 | const Query &Q) { |
| 535 | if (auto *CLHS = dyn_cast<Constant>(Op0)) { |
| 536 | if (auto *CRHS = dyn_cast<Constant>(Op1)) |
| 537 | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
| 538 | |
| 539 | // Canonicalize the constant to the RHS if this is a commutative operation. |
| 540 | if (Instruction::isCommutative(Opcode)) |
| 541 | std::swap(Op0, Op1); |
| 542 | } |
| 543 | return nullptr; |
| 544 | } |
| 545 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 546 | /// Given operands for an Add, see if we can fold the result. |
| 547 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 548 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 549 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 550 | if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q)) |
| 551 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 552 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 553 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 554 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 555 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 556 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 557 | // X + 0 -> X |
| 558 | if (match(Op1, m_Zero())) |
| 559 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 560 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 561 | // X + (Y - X) -> Y |
| 562 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 563 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 564 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 565 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 566 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 567 | return Y; |
| 568 | |
| 569 | // X + ~X -> -1 since ~X = -X-1 |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 570 | Type *Ty = Op0->getType(); |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 571 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 572 | match(Op1, m_Not(m_Specific(Op0)))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 573 | return Constant::getAllOnesValue(Ty); |
| 574 | |
| 575 | // add nsw/nuw (xor Y, signbit), signbit --> Y |
| 576 | // The no-wrapping add guarantees that the top bit will be set by the add. |
| 577 | // Therefore, the xor must be clearing the already set sign bit of Y. |
Craig Topper | 3a40a39 | 2017-03-30 22:21:16 +0000 | [diff] [blame] | 578 | if ((isNSW || isNUW) && match(Op1, m_SignBit()) && |
| 579 | match(Op0, m_Xor(m_Value(Y), m_SignBit()))) |
Sanjay Patel | fe67255 | 2017-02-18 21:59:09 +0000 | [diff] [blame] | 580 | return Y; |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 581 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 582 | /// i1 add -> xor. |
Craig Topper | aa5f524 | 2017-04-06 05:28:41 +0000 | [diff] [blame] | 583 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 584 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 585 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 586 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 587 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 588 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 589 | MaxRecurse)) |
| 590 | return V; |
| 591 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 592 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 593 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 594 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 595 | // only if B and C are equal. If B and C are equal then (since we assume |
| 596 | // that operands have already been simplified) "select(cond, B, C)" should |
| 597 | // have been simplified to the common value of B and C already. Analysing |
| 598 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 599 | // for threading over phi nodes. |
| 600 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 601 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 604 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 605 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 606 | const DominatorTree *DT, AssumptionCache *AC, |
| 607 | const Instruction *CxtI) { |
| 608 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 609 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 612 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 613 | /// |
| 614 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 615 | /// accumulates the total constant offset applied in the returned constant. It |
| 616 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 617 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 618 | /// |
| 619 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 620 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 621 | /// folding. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 622 | static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 623 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 624 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 625 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 626 | Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 627 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 628 | |
| 629 | // Even though we don't look through PHI nodes, we could be called on an |
| 630 | // instruction in an unreachable block, which may be on a cycle. |
| 631 | SmallPtrSet<Value *, 4> Visited; |
| 632 | Visited.insert(V); |
| 633 | do { |
| 634 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 635 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 636 | !GEP->accumulateConstantOffset(DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 637 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 638 | V = GEP->getPointerOperand(); |
| 639 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 640 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 641 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 642 | if (GA->isInterposable()) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 643 | break; |
| 644 | V = GA->getAliasee(); |
| 645 | } else { |
Hal Finkel | 2cac58f | 2016-07-11 03:37:59 +0000 | [diff] [blame] | 646 | if (auto CS = CallSite(V)) |
| 647 | if (Value *RV = CS.getReturnedArgOperand()) { |
| 648 | V = RV; |
| 649 | continue; |
| 650 | } |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 651 | break; |
| 652 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 653 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 654 | "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 655 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 656 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 657 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 658 | if (V->getType()->isVectorTy()) |
| 659 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 660 | OffsetIntPtr); |
| 661 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /// \brief Compute the constant difference between two pointer values. |
| 665 | /// If the difference is not a constant, returns zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 666 | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
| 667 | Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 668 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 669 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 670 | |
| 671 | // If LHS and RHS are not related via constant offsets to the same base |
| 672 | // value, there is nothing we can do here. |
| 673 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 674 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 675 | |
| 676 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 677 | // LHS - RHS |
| 678 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 679 | // = LHSOffset - RHSOffset |
| 680 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 681 | } |
| 682 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 683 | /// Given operands for a Sub, see if we can fold the result. |
| 684 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 685 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 686 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 687 | if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q)) |
| 688 | return C; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 689 | |
| 690 | // X - undef -> undef |
| 691 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 692 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 693 | return UndefValue::get(Op0->getType()); |
| 694 | |
| 695 | // X - 0 -> X |
| 696 | if (match(Op1, m_Zero())) |
| 697 | return Op0; |
| 698 | |
| 699 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 700 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 701 | return Constant::getNullValue(Op0->getType()); |
| 702 | |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 703 | // Is this a negation? |
| 704 | if (match(Op0, m_Zero())) { |
| 705 | // 0 - X -> 0 if the sub is NUW. |
| 706 | if (isNUW) |
| 707 | return Op0; |
| 708 | |
| 709 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 710 | APInt KnownZero(BitWidth, 0); |
| 711 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 712 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Craig Topper | 6856d34 | 2017-03-30 22:10:54 +0000 | [diff] [blame] | 713 | if (KnownZero.isMaxSignedValue()) { |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 714 | // Op1 is either 0 or the minimum signed value. If the sub is NSW, then |
| 715 | // Op1 must be 0 because negating the minimum signed value is undefined. |
| 716 | if (isNSW) |
| 717 | return Op0; |
| 718 | |
| 719 | // 0 - X -> X if X is 0 or the minimum signed value. |
| 720 | return Op1; |
| 721 | } |
| 722 | } |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 723 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 724 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 725 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 726 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 727 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 728 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 729 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 730 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 731 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 732 | // It does, we successfully reassociated! |
| 733 | ++NumReassoc; |
| 734 | return W; |
| 735 | } |
| 736 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 737 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 738 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 739 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 740 | // It does, we successfully reassociated! |
| 741 | ++NumReassoc; |
| 742 | return W; |
| 743 | } |
| 744 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 745 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 746 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 747 | // For example, X - (X + 1) -> -1 |
| 748 | X = Op0; |
| 749 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 750 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 751 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 752 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 753 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 754 | // It does, we successfully reassociated! |
| 755 | ++NumReassoc; |
| 756 | return W; |
| 757 | } |
| 758 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 759 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 760 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 761 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 762 | // It does, we successfully reassociated! |
| 763 | ++NumReassoc; |
| 764 | return W; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 769 | // For example, X - (X - Y) -> Y. |
| 770 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 771 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 772 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 773 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 774 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 775 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 776 | // It does, we successfully reassociated! |
| 777 | ++NumReassoc; |
| 778 | return W; |
| 779 | } |
| 780 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 781 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 782 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 783 | match(Op1, m_Trunc(m_Value(Y)))) |
| 784 | if (X->getType() == Y->getType()) |
| 785 | // See if "V === X - Y" simplifies. |
| 786 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 787 | // It does! Now see if "trunc V" simplifies. |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 788 | if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
| 789 | Q, MaxRecurse - 1)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 790 | // It does, return the simplified "trunc V". |
| 791 | return W; |
| 792 | |
| 793 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 794 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 795 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 796 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 797 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 798 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 799 | // i1 sub -> xor. |
Craig Topper | aa5f524 | 2017-04-06 05:28:41 +0000 | [diff] [blame] | 800 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 801 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 802 | return V; |
| 803 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 804 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 805 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 806 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 807 | // only if B and C are equal. If B and C are equal then (since we assume |
| 808 | // that operands have already been simplified) "select(cond, B, C)" should |
| 809 | // have been simplified to the common value of B and C already. Analysing |
| 810 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 811 | // for threading over phi nodes. |
| 812 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 813 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 816 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 817 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 818 | const DominatorTree *DT, AssumptionCache *AC, |
| 819 | const Instruction *CxtI) { |
| 820 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 821 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 824 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 825 | /// returns null. |
| 826 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 827 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 828 | if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q)) |
| 829 | return C; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 830 | |
| 831 | // fadd X, -0 ==> X |
| 832 | if (match(Op1, m_NegZero())) |
| 833 | return Op0; |
| 834 | |
| 835 | // fadd X, 0 ==> X, when we know X is not -0 |
| 836 | if (match(Op1, m_Zero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 837 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 838 | return Op0; |
| 839 | |
| 840 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 841 | // where nnan and ninf have to occur at least once somewhere in this |
| 842 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 843 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 844 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 845 | SubOp = Op1; |
| 846 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 847 | SubOp = Op0; |
| 848 | if (SubOp) { |
| 849 | Instruction *FSub = cast<Instruction>(SubOp); |
| 850 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 851 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 852 | return Constant::getNullValue(Op0->getType()); |
| 853 | } |
| 854 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 855 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 859 | /// returns null. |
| 860 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 861 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 862 | if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q)) |
| 863 | return C; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 864 | |
| 865 | // fsub X, 0 ==> X |
| 866 | if (match(Op1, m_Zero())) |
| 867 | return Op0; |
| 868 | |
| 869 | // fsub X, -0 ==> X, when we know X is not -0 |
| 870 | if (match(Op1, m_NegZero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 871 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 872 | return Op0; |
| 873 | |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 874 | // fsub -0.0, (fsub -0.0, X) ==> X |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 875 | Value *X; |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 876 | if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 877 | return X; |
| 878 | |
| 879 | // 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] | 880 | if (FMF.noSignedZeros() && match(Op0, m_AnyZero()) && |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 881 | match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 882 | return X; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 883 | |
Benjamin Kramer | 228680d | 2015-06-14 21:01:20 +0000 | [diff] [blame] | 884 | // fsub nnan x, x ==> 0.0 |
| 885 | if (FMF.noNaNs() && Op0 == Op1) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 886 | return Constant::getNullValue(Op0->getType()); |
| 887 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 888 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 891 | /// Given the operands for an FMul, see if we can fold the result |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 892 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 893 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 894 | if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q)) |
| 895 | return C; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 896 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 897 | // fmul X, 1.0 ==> X |
| 898 | if (match(Op1, m_FPOne())) |
| 899 | return Op0; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 900 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 901 | // fmul nnan nsz X, 0 ==> 0 |
| 902 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 903 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 904 | |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 905 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 908 | /// Given operands for a Mul, see if we can fold the result. |
| 909 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 910 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 911 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 912 | if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q)) |
| 913 | return C; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 914 | |
| 915 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 916 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 917 | return Constant::getNullValue(Op0->getType()); |
| 918 | |
| 919 | // X * 0 -> 0 |
| 920 | if (match(Op1, m_Zero())) |
| 921 | return Op1; |
| 922 | |
| 923 | // X * 1 -> X |
| 924 | if (match(Op1, m_One())) |
| 925 | return Op0; |
| 926 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 927 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 928 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 929 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 930 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 931 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 932 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 933 | // i1 mul -> and. |
Craig Topper | 2f1e1c3 | 2017-04-06 17:33:37 +0000 | [diff] [blame] | 934 | if (MaxRecurse && Op0->getType()->getScalarType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 935 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 936 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 937 | |
| 938 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 939 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 940 | MaxRecurse)) |
| 941 | return V; |
| 942 | |
| 943 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 944 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 945 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 946 | return V; |
| 947 | |
| 948 | // If the operation is with the result of a select instruction, check whether |
| 949 | // operating on either branch of the select always yields the same value. |
| 950 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 951 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 952 | MaxRecurse)) |
| 953 | return V; |
| 954 | |
| 955 | // If the operation is with the result of a phi instruction, check whether |
| 956 | // operating on all incoming values of the phi always yields the same value. |
| 957 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 958 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 959 | MaxRecurse)) |
| 960 | return V; |
| 961 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 962 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 965 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 966 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 967 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 968 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 969 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 970 | return ::SimplifyFAddInst(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 | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 975 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 976 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 977 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 978 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 979 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 980 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 983 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 984 | const DataLayout &DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 985 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 986 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 987 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 988 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 989 | RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 992 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 993 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 994 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 995 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 996 | return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 997 | RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1000 | /// Check for common or similar folds of integer division or integer remainder. |
| 1001 | static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) { |
| 1002 | Type *Ty = Op0->getType(); |
| 1003 | |
| 1004 | // X / undef -> undef |
| 1005 | // X % undef -> undef |
| 1006 | if (match(Op1, m_Undef())) |
| 1007 | return Op1; |
| 1008 | |
| 1009 | // X / 0 -> undef |
| 1010 | // X % 0 -> undef |
| 1011 | // We don't need to preserve faults! |
| 1012 | if (match(Op1, m_Zero())) |
| 1013 | return UndefValue::get(Ty); |
| 1014 | |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 1015 | // If any element of a constant divisor vector is zero, the whole op is undef. |
| 1016 | auto *Op1C = dyn_cast<Constant>(Op1); |
| 1017 | if (Op1C && Ty->isVectorTy()) { |
| 1018 | unsigned NumElts = Ty->getVectorNumElements(); |
| 1019 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1020 | Constant *Elt = Op1C->getAggregateElement(i); |
| 1021 | if (Elt && Elt->isNullValue()) |
| 1022 | return UndefValue::get(Ty); |
| 1023 | } |
| 1024 | } |
| 1025 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1026 | // undef / X -> 0 |
| 1027 | // undef % X -> 0 |
| 1028 | if (match(Op0, m_Undef())) |
| 1029 | return Constant::getNullValue(Ty); |
| 1030 | |
| 1031 | // 0 / X -> 0 |
| 1032 | // 0 % X -> 0 |
| 1033 | if (match(Op0, m_Zero())) |
| 1034 | return Op0; |
| 1035 | |
| 1036 | // X / X -> 1 |
| 1037 | // X % X -> 0 |
| 1038 | if (Op0 == Op1) |
| 1039 | return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty); |
| 1040 | |
| 1041 | // X / 1 -> X |
| 1042 | // X % 1 -> 0 |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 1043 | // If this is a boolean op (single-bit element type), we can't have |
| 1044 | // division-by-zero or remainder-by-zero, so assume the divisor is 1. |
| 1045 | if (match(Op1, m_One()) || Ty->getScalarType()->isIntegerTy(1)) |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1046 | return IsDiv ? Op0 : Constant::getNullValue(Ty); |
| 1047 | |
| 1048 | return nullptr; |
| 1049 | } |
| 1050 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1051 | /// Given operands for an SDiv or UDiv, see if we can fold the result. |
| 1052 | /// If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 1053 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1054 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1055 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1056 | return C; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1057 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1058 | if (Value *V = simplifyDivRem(Op0, Op1, true)) |
| 1059 | return V; |
| 1060 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1061 | bool isSigned = Opcode == Instruction::SDiv; |
| 1062 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1063 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1064 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1065 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1066 | 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] | 1067 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1068 | // If the Mul knows it does not overflow, then we are good to go. |
| 1069 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1070 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1071 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1072 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1073 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1074 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1075 | return X; |
| 1076 | } |
| 1077 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1078 | // (X rem Y) / Y -> 0 |
| 1079 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1080 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1081 | return Constant::getNullValue(Op0->getType()); |
| 1082 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1083 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1084 | ConstantInt *C1, *C2; |
| 1085 | if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
| 1086 | match(Op1, m_ConstantInt(C2))) { |
| 1087 | bool Overflow; |
| 1088 | C1->getValue().umul_ov(C2->getValue(), Overflow); |
| 1089 | if (Overflow) |
| 1090 | return Constant::getNullValue(Op0->getType()); |
| 1091 | } |
| 1092 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1093 | // If the operation is with the result of a select instruction, check whether |
| 1094 | // operating on either branch of the select always yields the same value. |
| 1095 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1096 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1097 | return V; |
| 1098 | |
| 1099 | // If the operation is with the result of a phi instruction, check whether |
| 1100 | // operating on all incoming values of the phi always yields the same value. |
| 1101 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1102 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1103 | return V; |
| 1104 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1105 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1108 | /// Given operands for an SDiv, see if we can fold the result. |
| 1109 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1110 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1111 | unsigned MaxRecurse) { |
| 1112 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1113 | return V; |
| 1114 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1115 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1118 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1119 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1120 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1121 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1122 | return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1123 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1126 | /// Given operands for a UDiv, see if we can fold the result. |
| 1127 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1128 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1129 | unsigned MaxRecurse) { |
| 1130 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1131 | return V; |
| 1132 | |
David Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 1133 | // udiv %V, C -> 0 if %V < C |
| 1134 | if (MaxRecurse) { |
| 1135 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1136 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1137 | if (C->isAllOnesValue()) { |
| 1138 | return Constant::getNullValue(Op0->getType()); |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1143 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1146 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1147 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1148 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1149 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1150 | return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1151 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1154 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1155 | const Query &Q, unsigned) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1156 | if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q)) |
| 1157 | return C; |
| 1158 | |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1159 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1160 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1161 | return Op0; |
| 1162 | |
| 1163 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1164 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1165 | return Op1; |
| 1166 | |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 1167 | // X / 1.0 -> X |
| 1168 | if (match(Op1, m_FPOne())) |
| 1169 | return Op0; |
| 1170 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1171 | // 0 / X -> 0 |
| 1172 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1173 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1174 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1175 | return Op0; |
| 1176 | |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1177 | if (FMF.noNaNs()) { |
| 1178 | // X / X -> 1.0 is legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1179 | if (Op0 == Op1) |
| 1180 | return ConstantFP::get(Op0->getType(), 1.0); |
| 1181 | |
| 1182 | // -X / X -> -1.0 and |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1183 | // X / -X -> -1.0 are legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1184 | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
| 1185 | if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && |
| 1186 | BinaryOperator::getFNegArgument(Op0) == Op1) || |
| 1187 | (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && |
| 1188 | BinaryOperator::getFNegArgument(Op1) == Op0)) |
| 1189 | return ConstantFP::get(Op0->getType(), -1.0); |
| 1190 | } |
| 1191 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1192 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1195 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1196 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1197 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1198 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1199 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1200 | return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1201 | RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1204 | /// Given operands for an SRem or URem, see if we can fold the result. |
| 1205 | /// If not, this returns null. |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1206 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1207 | const Query &Q, unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1208 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1209 | return C; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1210 | |
Sanjay Patel | 0cb2ee9 | 2017-03-06 19:08:35 +0000 | [diff] [blame] | 1211 | if (Value *V = simplifyDivRem(Op0, Op1, false)) |
| 1212 | return V; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1213 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1214 | // (X % Y) % Y -> X % Y |
| 1215 | if ((Opcode == Instruction::SRem && |
| 1216 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1217 | (Opcode == Instruction::URem && |
| 1218 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1219 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1220 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1221 | // If the operation is with the result of a select instruction, check whether |
| 1222 | // operating on either branch of the select always yields the same value. |
| 1223 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1224 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1225 | return V; |
| 1226 | |
| 1227 | // If the operation is with the result of a phi instruction, check whether |
| 1228 | // operating on all incoming values of the phi always yields the same value. |
| 1229 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1230 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1231 | return V; |
| 1232 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1233 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1236 | /// Given operands for an SRem, see if we can fold the result. |
| 1237 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1238 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1239 | unsigned MaxRecurse) { |
| 1240 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1241 | return V; |
| 1242 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1243 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1246 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1247 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1248 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1249 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1250 | return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1251 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1254 | /// Given operands for a URem, see if we can fold the result. |
| 1255 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1256 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1257 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1258 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1259 | return V; |
| 1260 | |
David Majnemer | 8c0e62f | 2017-01-06 21:23:51 +0000 | [diff] [blame] | 1261 | // urem %V, C -> %V if %V < C |
| 1262 | if (MaxRecurse) { |
| 1263 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1264 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1265 | if (C->isAllOnesValue()) { |
| 1266 | return Op0; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1271 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1274 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1275 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1276 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1277 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1278 | return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1279 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1282 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1283 | const Query &Q, unsigned) { |
| 1284 | if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q)) |
| 1285 | return C; |
| 1286 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1287 | // undef % X -> undef (the undef could be a snan). |
| 1288 | if (match(Op0, m_Undef())) |
| 1289 | return Op0; |
| 1290 | |
| 1291 | // X % undef -> undef |
| 1292 | if (match(Op1, m_Undef())) |
| 1293 | return Op1; |
| 1294 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1295 | // 0 % X -> 0 |
| 1296 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1297 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1298 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1299 | return Op0; |
| 1300 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1301 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1304 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1305 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1306 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1307 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1308 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1309 | return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1310 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1313 | /// Returns true if a shift by \c Amount always yields undef. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1314 | static bool isUndefShift(Value *Amount) { |
| 1315 | Constant *C = dyn_cast<Constant>(Amount); |
| 1316 | if (!C) |
| 1317 | return false; |
| 1318 | |
| 1319 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1320 | if (isa<UndefValue>(C)) |
| 1321 | return true; |
| 1322 | |
| 1323 | // Shifting by the bitwidth or more is undefined. |
| 1324 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1325 | if (CI->getValue().getLimitedValue() >= |
| 1326 | CI->getType()->getScalarSizeInBits()) |
| 1327 | return true; |
| 1328 | |
| 1329 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1330 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1331 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1332 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1333 | return false; |
| 1334 | return true; |
| 1335 | } |
| 1336 | |
| 1337 | return false; |
| 1338 | } |
| 1339 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1340 | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
| 1341 | /// If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1342 | static Value *SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0, |
| 1343 | Value *Op1, const Query &Q, unsigned MaxRecurse) { |
| 1344 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
| 1345 | return C; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1346 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1347 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1348 | if (match(Op0, m_Zero())) |
| 1349 | return Op0; |
| 1350 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1351 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1352 | if (match(Op1, m_Zero())) |
| 1353 | return Op0; |
| 1354 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1355 | // Fold undefined shifts. |
| 1356 | if (isUndefShift(Op1)) |
| 1357 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1358 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1359 | // If the operation is with the result of a select instruction, check whether |
| 1360 | // operating on either branch of the select always yields the same value. |
| 1361 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1362 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1363 | return V; |
| 1364 | |
| 1365 | // If the operation is with the result of a phi instruction, check whether |
| 1366 | // operating on all incoming values of the phi always yields the same value. |
| 1367 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1368 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1369 | return V; |
| 1370 | |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1371 | // If any bits in the shift amount make that value greater than or equal to |
| 1372 | // the number of bits in the type, the shift is undefined. |
| 1373 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 1374 | APInt KnownZero(BitWidth, 0); |
| 1375 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1376 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1377 | if (KnownOne.getLimitedValue() >= BitWidth) |
| 1378 | return UndefValue::get(Op0->getType()); |
| 1379 | |
| 1380 | // If all valid bits in the shift amount are known zero, the first operand is |
| 1381 | // unchanged. |
| 1382 | unsigned NumValidShiftBits = Log2_32_Ceil(BitWidth); |
| 1383 | APInt ShiftAmountMask = APInt::getLowBitsSet(BitWidth, NumValidShiftBits); |
| 1384 | if ((KnownZero & ShiftAmountMask) == ShiftAmountMask) |
| 1385 | return Op0; |
| 1386 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1387 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1390 | /// \brief Given operands for an Shl, LShr or AShr, see if we can |
| 1391 | /// fold the result. If not, this returns null. |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1392 | static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, |
| 1393 | Value *Op1, bool isExact, const Query &Q, |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1394 | unsigned MaxRecurse) { |
| 1395 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1396 | return V; |
| 1397 | |
| 1398 | // X >> X -> 0 |
| 1399 | if (Op0 == Op1) |
| 1400 | return Constant::getNullValue(Op0->getType()); |
| 1401 | |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1402 | // undef >> X -> 0 |
| 1403 | // undef >> X -> undef (if it's exact) |
| 1404 | if (match(Op0, m_Undef())) |
| 1405 | return isExact ? Op0 : Constant::getNullValue(Op0->getType()); |
| 1406 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1407 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1408 | if (isExact) { |
| 1409 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 1410 | APInt Op0KnownZero(BitWidth, 0); |
| 1411 | APInt Op0KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1412 | computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC, |
| 1413 | Q.CxtI, Q.DT); |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1414 | if (Op0KnownOne[0]) |
| 1415 | return Op0; |
| 1416 | } |
| 1417 | |
| 1418 | return nullptr; |
| 1419 | } |
| 1420 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1421 | /// Given operands for an Shl, see if we can fold the result. |
| 1422 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1423 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1424 | const Query &Q, unsigned MaxRecurse) { |
| 1425 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1426 | return V; |
| 1427 | |
| 1428 | // undef << X -> 0 |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1429 | // undef << X -> undef if (if it's NSW/NUW) |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1430 | if (match(Op0, m_Undef())) |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1431 | return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType()); |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1433 | // (X >> A) << A -> X |
| 1434 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1435 | 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] | 1436 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1437 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1440 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1441 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1442 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1443 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1444 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1445 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1448 | /// Given operands for an LShr, see if we can fold the result. |
| 1449 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1450 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1451 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1452 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1453 | MaxRecurse)) |
| 1454 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1456 | // (X << A) >> A -> X |
| 1457 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1458 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1459 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1460 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1461 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1464 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1465 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1466 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1467 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1468 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1469 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1470 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1473 | /// Given operands for an AShr, see if we can fold the result. |
| 1474 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1475 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1476 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1477 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1478 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1479 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1480 | |
| 1481 | // all ones >>a X -> all ones |
| 1482 | if (match(Op0, m_AllOnes())) |
| 1483 | return Op0; |
| 1484 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1485 | // (X << A) >> A -> X |
| 1486 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1487 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1488 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1489 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1490 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1491 | 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] | 1492 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1493 | return Op0; |
| 1494 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1495 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1498 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1499 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1500 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1501 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1502 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1503 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1504 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1507 | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
| 1508 | ICmpInst *UnsignedICmp, bool IsAnd) { |
| 1509 | Value *X, *Y; |
| 1510 | |
| 1511 | ICmpInst::Predicate EqPred; |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1512 | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
| 1513 | !ICmpInst::isEquality(EqPred)) |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1514 | return nullptr; |
| 1515 | |
| 1516 | ICmpInst::Predicate UnsignedPred; |
| 1517 | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
| 1518 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1519 | ; |
| 1520 | else if (match(UnsignedICmp, |
| 1521 | m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) && |
| 1522 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1523 | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
| 1524 | else |
| 1525 | return nullptr; |
| 1526 | |
| 1527 | // X < Y && Y != 0 --> X < Y |
| 1528 | // X < Y || Y != 0 --> Y != 0 |
| 1529 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE) |
| 1530 | return IsAnd ? UnsignedICmp : ZeroICmp; |
| 1531 | |
| 1532 | // X >= Y || Y != 0 --> true |
| 1533 | // X >= Y || Y == 0 --> X >= Y |
| 1534 | if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) { |
| 1535 | if (EqPred == ICmpInst::ICMP_NE) |
| 1536 | return getTrue(UnsignedICmp->getType()); |
| 1537 | return UnsignedICmp; |
| 1538 | } |
| 1539 | |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1540 | // X < Y && Y == 0 --> false |
| 1541 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ && |
| 1542 | IsAnd) |
| 1543 | return getFalse(UnsignedICmp->getType()); |
| 1544 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1545 | return nullptr; |
| 1546 | } |
| 1547 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1548 | /// Commuted variants are assumed to be handled by calling this function again |
| 1549 | /// with the parameters swapped. |
| 1550 | static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1551 | ICmpInst::Predicate Pred0, Pred1; |
| 1552 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1553 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1554 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1555 | return nullptr; |
| 1556 | |
| 1557 | // We have (icmp Pred0, A, B) & (icmp Pred1, A, B). |
| 1558 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1559 | // can eliminate Op1 from this 'and'. |
| 1560 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1561 | return Op0; |
| 1562 | |
| 1563 | // Check for any combination of predicates that are guaranteed to be disjoint. |
| 1564 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1565 | (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) || |
| 1566 | (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) || |
| 1567 | (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)) |
| 1568 | return getFalse(Op0->getType()); |
| 1569 | |
| 1570 | return nullptr; |
| 1571 | } |
| 1572 | |
| 1573 | /// Commuted variants are assumed to be handled by calling this function again |
| 1574 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1575 | static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1576 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true)) |
| 1577 | return X; |
| 1578 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1579 | if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1)) |
| 1580 | return X; |
| 1581 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1582 | // Look for this pattern: (icmp V, C0) & (icmp V, C1)). |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1583 | Type *ITy = Op0->getType(); |
| 1584 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1585 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1586 | Value *V; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1587 | if (match(Op0, m_ICmp(Pred0, m_Value(V), m_APInt(C0))) && |
| 1588 | match(Op1, m_ICmp(Pred1, m_Specific(V), m_APInt(C1)))) { |
| 1589 | // Make a constant range that's the intersection of the two icmp ranges. |
| 1590 | // If the intersection is empty, we know that the result is false. |
| 1591 | auto Range0 = ConstantRange::makeAllowedICmpRegion(Pred0, *C0); |
| 1592 | auto Range1 = ConstantRange::makeAllowedICmpRegion(Pred1, *C1); |
| 1593 | if (Range0.intersectWith(Range1).isEmptySet()) |
| 1594 | return getFalse(ITy); |
| 1595 | } |
| 1596 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1597 | // (icmp (add V, C0), C1) & (icmp V, C0) |
| 1598 | 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] | 1599 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1600 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1601 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1602 | return nullptr; |
| 1603 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1604 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1605 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1606 | return nullptr; |
| 1607 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1608 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1609 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1610 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1611 | const APInt Delta = *C1 - *C0; |
| 1612 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1613 | if (Delta == 2) { |
| 1614 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1615 | return getFalse(ITy); |
| 1616 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1617 | return getFalse(ITy); |
| 1618 | } |
| 1619 | if (Delta == 1) { |
| 1620 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1621 | return getFalse(ITy); |
| 1622 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1623 | return getFalse(ITy); |
| 1624 | } |
| 1625 | } |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1626 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1627 | if (Delta == 2) |
| 1628 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1629 | return getFalse(ITy); |
| 1630 | if (Delta == 1) |
| 1631 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1632 | return getFalse(ITy); |
| 1633 | } |
| 1634 | |
| 1635 | return nullptr; |
| 1636 | } |
| 1637 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1638 | /// Given operands for an And, see if we can fold the result. |
| 1639 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1640 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1641 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1642 | if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q)) |
| 1643 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1645 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1646 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1647 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1649 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1650 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1651 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1652 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1653 | // X & 0 = 0 |
| 1654 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1655 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1656 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1657 | // X & -1 = X |
| 1658 | if (match(Op1, m_AllOnes())) |
| 1659 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1661 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1662 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1663 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1664 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1666 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1667 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1668 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1669 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1670 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1672 | // A & (A | ?) = A |
| 1673 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1674 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1675 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1676 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1677 | // A & (-A) = A if A is a power of two or zero. |
| 1678 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1679 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1680 | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1681 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1682 | return Op0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1683 | if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1684 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1685 | return Op1; |
| 1686 | } |
| 1687 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1688 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1689 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1690 | if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS)) |
| 1691 | return V; |
| 1692 | if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS)) |
| 1693 | return V; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1697 | // The compares may be hidden behind casts. Look through those and try the |
| 1698 | // same folds as above. |
| 1699 | auto *Cast0 = dyn_cast<CastInst>(Op0); |
| 1700 | auto *Cast1 = dyn_cast<CastInst>(Op1); |
| 1701 | if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() && |
| 1702 | Cast0->getSrcTy() == Cast1->getSrcTy()) { |
| 1703 | auto *Cmp0 = dyn_cast<ICmpInst>(Cast0->getOperand(0)); |
| 1704 | auto *Cmp1 = dyn_cast<ICmpInst>(Cast1->getOperand(0)); |
| 1705 | if (Cmp0 && Cmp1) { |
| 1706 | Instruction::CastOps CastOpc = Cast0->getOpcode(); |
| 1707 | Type *ResultType = Cast0->getType(); |
| 1708 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp0, Cmp1))) |
| 1709 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1710 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp1, Cmp0))) |
| 1711 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1712 | } |
| 1713 | } |
| 1714 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1715 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1716 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1717 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1718 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1719 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1720 | // And distributes over Or. Try some generic simplifications based on this. |
| 1721 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1722 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1723 | return V; |
| 1724 | |
| 1725 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1726 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1727 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1728 | return V; |
| 1729 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1730 | // If the operation is with the result of a select instruction, check whether |
| 1731 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1732 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1733 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1734 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1735 | return V; |
| 1736 | |
| 1737 | // If the operation is with the result of a phi instruction, check whether |
| 1738 | // 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] | 1739 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1740 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1741 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1742 | return V; |
| 1743 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1744 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1747 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1748 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1749 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1750 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1751 | return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1752 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1755 | /// Commuted variants are assumed to be handled by calling this function again |
| 1756 | /// with the parameters swapped. |
| 1757 | static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1758 | ICmpInst::Predicate Pred0, Pred1; |
| 1759 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1760 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1761 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1762 | return nullptr; |
| 1763 | |
| 1764 | // We have (icmp Pred0, A, B) | (icmp Pred1, A, B). |
| 1765 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1766 | // can eliminate Op0 from this 'or'. |
| 1767 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1768 | return Op1; |
| 1769 | |
| 1770 | // Check for any combination of predicates that cover the entire range of |
| 1771 | // possibilities. |
| 1772 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1773 | (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) || |
| 1774 | (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) || |
| 1775 | (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE)) |
| 1776 | return getTrue(Op0->getType()); |
| 1777 | |
| 1778 | return nullptr; |
| 1779 | } |
| 1780 | |
| 1781 | /// Commuted variants are assumed to be handled by calling this function again |
| 1782 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1783 | static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1784 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false)) |
| 1785 | return X; |
| 1786 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1787 | if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1)) |
| 1788 | return X; |
| 1789 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1790 | // (icmp (add V, C0), C1) | (icmp V, C0) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1791 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1792 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1793 | Value *V; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1794 | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1795 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1796 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1797 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
| 1798 | return nullptr; |
| 1799 | |
| 1800 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1801 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1802 | return nullptr; |
| 1803 | |
| 1804 | Type *ITy = Op0->getType(); |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1805 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1806 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1807 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1808 | const APInt Delta = *C1 - *C0; |
| 1809 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1810 | if (Delta == 2) { |
| 1811 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1812 | return getTrue(ITy); |
| 1813 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1814 | return getTrue(ITy); |
| 1815 | } |
| 1816 | if (Delta == 1) { |
| 1817 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1818 | return getTrue(ITy); |
| 1819 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1820 | return getTrue(ITy); |
| 1821 | } |
| 1822 | } |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1823 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1824 | if (Delta == 2) |
| 1825 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1826 | return getTrue(ITy); |
| 1827 | if (Delta == 1) |
| 1828 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1829 | return getTrue(ITy); |
| 1830 | } |
| 1831 | |
| 1832 | return nullptr; |
| 1833 | } |
| 1834 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1835 | /// Given operands for an Or, see if we can fold the result. |
| 1836 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1837 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1838 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1839 | if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q)) |
| 1840 | return C; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1842 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1843 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1844 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1846 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1847 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1848 | return Op0; |
| 1849 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1850 | // X | 0 = X |
| 1851 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1852 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1853 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1854 | // X | -1 = -1 |
| 1855 | if (match(Op1, m_AllOnes())) |
| 1856 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1858 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1859 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1860 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1861 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1863 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1864 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1865 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1866 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1867 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1869 | // A | (A & ?) = A |
| 1870 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1871 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1872 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1873 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1874 | // ~(A & ?) | A = -1 |
| 1875 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1876 | (A == Op1 || B == Op1)) |
| 1877 | return Constant::getAllOnesValue(Op1->getType()); |
| 1878 | |
| 1879 | // A | ~(A & ?) = -1 |
| 1880 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1881 | (A == Op0 || B == Op0)) |
| 1882 | return Constant::getAllOnesValue(Op0->getType()); |
| 1883 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1884 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1885 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1886 | if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS)) |
| 1887 | return V; |
| 1888 | if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS)) |
| 1889 | return V; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1893 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1894 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1895 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1896 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1897 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1898 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1899 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1900 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1901 | return V; |
| 1902 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1903 | // If the operation is with the result of a select instruction, check whether |
| 1904 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1905 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1906 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1907 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1908 | return V; |
| 1909 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1910 | // (A & C)|(B & D) |
| 1911 | Value *C = nullptr, *D = nullptr; |
| 1912 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1913 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1914 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1915 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1916 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1917 | // (A & C1)|(B & C2) |
| 1918 | // If we have: ((V + N) & C1) | (V & C2) |
| 1919 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1920 | // replace with V+N. |
| 1921 | Value *V1, *V2; |
| 1922 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1923 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1924 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1925 | if (V1 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1926 | 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] | 1927 | return A; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1928 | if (V2 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1929 | 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] | 1930 | return A; |
| 1931 | } |
| 1932 | // Or commutes, try both ways. |
| 1933 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1934 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1935 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1936 | if (V1 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1937 | 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] | 1938 | return B; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1939 | if (V2 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1940 | 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] | 1941 | return B; |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1946 | // If the operation is with the result of a phi instruction, check whether |
| 1947 | // 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] | 1948 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1949 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1950 | return V; |
| 1951 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1952 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1955 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1956 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1957 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1958 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1959 | return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1960 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1961 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1962 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1963 | /// Given operands for a Xor, see if we can fold the result. |
| 1964 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1965 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1966 | unsigned MaxRecurse) { |
Sanjay Patel | 8b5ad3f | 2017-04-01 19:05:11 +0000 | [diff] [blame] | 1967 | if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q)) |
| 1968 | return C; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1969 | |
| 1970 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1971 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1972 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1973 | |
| 1974 | // A ^ 0 = A |
| 1975 | if (match(Op1, m_Zero())) |
| 1976 | return Op0; |
| 1977 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1978 | // A ^ A = 0 |
| 1979 | if (Op0 == Op1) |
| 1980 | return Constant::getNullValue(Op0->getType()); |
| 1981 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1982 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1983 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1984 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1985 | return Constant::getAllOnesValue(Op0->getType()); |
| 1986 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1987 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1988 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 1989 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1990 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1991 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1992 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1993 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1994 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1995 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1996 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1997 | // have been simplified to the common value of B and C already. Analysing |
| 1998 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1999 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2000 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2001 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2004 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2005 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2006 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2007 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2008 | return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2009 | RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2012 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2013 | return CmpInst::makeCmpResultType(Op->getType()); |
| 2014 | } |
| 2015 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 2016 | /// Rummage around inside V looking for something equivalent to the comparison |
| 2017 | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
| 2018 | /// Helper function for analyzing max/min idioms. |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2019 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 2020 | Value *LHS, Value *RHS) { |
| 2021 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 2022 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2023 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2024 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 2025 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2026 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2027 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 2028 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 2029 | return Cmp; |
| 2030 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 2031 | LHS == CmpRHS && RHS == CmpLHS) |
| 2032 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2033 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2036 | // A significant optimization not implemented here is assuming that alloca |
| 2037 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 2038 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 2039 | // conservative approach. |
| 2040 | // |
| 2041 | // This is inspired in part by C++11 5.10p1: |
| 2042 | // "Two pointers of the same type compare equal if and only if they are both |
| 2043 | // null, both point to the same function, or both represent the same |
| 2044 | // address." |
| 2045 | // |
| 2046 | // This is pretty permissive. |
| 2047 | // |
| 2048 | // It's also partly due to C11 6.5.9p6: |
| 2049 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 2050 | // pointers to the same object (including a pointer to an object and a |
| 2051 | // subobject at its beginning) or function, both are pointers to one past the |
| 2052 | // last element of the same array object, or one is a pointer to one past the |
| 2053 | // 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] | 2054 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2055 | // object in the address space.) |
| 2056 | // |
| 2057 | // C11's version is more restrictive, however there's no reason why an argument |
| 2058 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 2059 | // equal to the beginning of a stack object in the callee. |
| 2060 | // |
| 2061 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 2062 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 2063 | // this optimization. |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2064 | static Constant * |
| 2065 | computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 2066 | const DominatorTree *DT, CmpInst::Predicate Pred, |
| 2067 | const Instruction *CxtI, Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2068 | // First, skip past any trivial no-ops. |
| 2069 | LHS = LHS->stripPointerCasts(); |
| 2070 | RHS = RHS->stripPointerCasts(); |
| 2071 | |
| 2072 | // A non-null pointer is not equal to a null pointer. |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2073 | if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2074 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 2075 | return ConstantInt::get(GetCompareTy(LHS), |
| 2076 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2077 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2078 | // We can only fold certain predicates on pointer comparisons. |
| 2079 | switch (Pred) { |
| 2080 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2081 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2082 | |
| 2083 | // Equality comaprisons are easy to fold. |
| 2084 | case CmpInst::ICMP_EQ: |
| 2085 | case CmpInst::ICMP_NE: |
| 2086 | break; |
| 2087 | |
| 2088 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 2089 | // a GEP only protects against unsigned wrapping. |
| 2090 | case CmpInst::ICMP_UGT: |
| 2091 | case CmpInst::ICMP_UGE: |
| 2092 | case CmpInst::ICMP_ULT: |
| 2093 | case CmpInst::ICMP_ULE: |
| 2094 | // However, we have to switch them to their signed variants to handle |
| 2095 | // negative indices from the base pointer. |
| 2096 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 2097 | break; |
| 2098 | } |
| 2099 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2100 | // Strip off any constant offsets so that we can reason about them. |
| 2101 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 2102 | // here and compare base addresses like AliasAnalysis does, however there are |
| 2103 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 2104 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 2105 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2106 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 2107 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2108 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2109 | // If LHS and RHS are related via constant offsets to the same base |
| 2110 | // value, we can replace it with an icmp which just compares the offsets. |
| 2111 | if (LHS == RHS) |
| 2112 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2113 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2114 | // Various optimizations for (in)equality comparisons. |
| 2115 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 2116 | // Different non-empty allocations that exist at the same time have |
| 2117 | // different addresses (if the program can tell). Global variables always |
| 2118 | // exist, so they always exist during the lifetime of each other and all |
| 2119 | // allocas. Two different allocas usually have different addresses... |
| 2120 | // |
| 2121 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 2122 | // allocas, they may have the same address. It's tempting to reduce the |
| 2123 | // scope of the problem by only looking at *static* allocas here. That would |
| 2124 | // cover the majority of allocas while significantly reducing the likelihood |
| 2125 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 2126 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 2127 | // an entry block. Also, if we have a block that's not attached to a |
| 2128 | // function, we can't tell if it's "static" under the current definition. |
| 2129 | // Theoretically, this problem could be fixed by creating a new kind of |
| 2130 | // instruction kind specifically for static allocas. Such a new instruction |
| 2131 | // could be required to be at the top of the entry block, thus preventing it |
| 2132 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 2133 | // convert regular allocas into these special allocas. It'd be nifty. |
| 2134 | // However, until then, this problem remains open. |
| 2135 | // |
| 2136 | // So, we'll assume that two non-empty allocas have different addresses |
| 2137 | // for now. |
| 2138 | // |
| 2139 | // With all that, if the offsets are within the bounds of their allocations |
| 2140 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 2141 | // allocations aren't the same, the pointers are not equal. |
| 2142 | // |
| 2143 | // Note that it's not necessary to check for LHS being a global variable |
| 2144 | // address, due to canonicalization and constant folding. |
| 2145 | if (isa<AllocaInst>(LHS) && |
| 2146 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2147 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 2148 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2149 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2150 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2151 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 2152 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2153 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 2154 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2155 | if (!LHSOffsetValue.isNegative() && |
| 2156 | !RHSOffsetValue.isNegative() && |
| 2157 | LHSOffsetValue.ult(LHSSize) && |
| 2158 | RHSOffsetValue.ult(RHSSize)) { |
| 2159 | return ConstantInt::get(GetCompareTy(LHS), |
| 2160 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | // Repeat the above check but this time without depending on DataLayout |
| 2165 | // or being able to compute a precise size. |
| 2166 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 2167 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 2168 | LHSOffset->isNullValue() && |
| 2169 | RHSOffset->isNullValue()) |
| 2170 | return ConstantInt::get(GetCompareTy(LHS), |
| 2171 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2172 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2173 | |
| 2174 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2175 | // equality comparisons concerning the result. We avoid walking the whole |
| 2176 | // chain again by starting where the last calls to |
| 2177 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2178 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2179 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2180 | if (LHS == RHS) |
| 2181 | return ConstantExpr::getICmp(Pred, |
| 2182 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2183 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2184 | |
| 2185 | // If one side of the equality comparison must come from a noalias call |
| 2186 | // (meaning a system memory allocation function), and the other side must |
| 2187 | // come from a pointer that cannot overlap with dynamically-allocated |
| 2188 | // memory within the lifetime of the current function (allocas, byval |
| 2189 | // arguments, globals), then determine the comparison result here. |
| 2190 | SmallVector<Value *, 8> LHSUObjs, RHSUObjs; |
| 2191 | GetUnderlyingObjects(LHS, LHSUObjs, DL); |
| 2192 | GetUnderlyingObjects(RHS, RHSUObjs, DL); |
| 2193 | |
| 2194 | // Is the set of underlying objects all noalias calls? |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2195 | auto IsNAC = [](ArrayRef<Value *> Objects) { |
| 2196 | return all_of(Objects, isNoAliasCall); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2197 | }; |
| 2198 | |
| 2199 | // 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] | 2200 | // noalias calls. For allocas, we consider only static ones (dynamic |
| 2201 | // allocas might be transformed into calls to malloc not simultaneously |
| 2202 | // live with the compared-to allocation). For globals, we exclude symbols |
| 2203 | // that might be resolve lazily to symbols in another dynamically-loaded |
| 2204 | // library (and, thus, could be malloc'ed by the implementation). |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2205 | auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) { |
| 2206 | return all_of(Objects, [](Value *V) { |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2207 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 2208 | return AI->getParent() && AI->getFunction() && AI->isStaticAlloca(); |
| 2209 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 2210 | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 2211 | GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2212 | !GV->isThreadLocal(); |
| 2213 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2214 | return A->hasByValAttr(); |
| 2215 | return false; |
| 2216 | }); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2217 | }; |
| 2218 | |
| 2219 | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || |
| 2220 | (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) |
| 2221 | return ConstantInt::get(GetCompareTy(LHS), |
| 2222 | !CmpInst::isTrueWhenEqual(Pred)); |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2223 | |
| 2224 | // Fold comparisons for non-escaping pointer even if the allocation call |
| 2225 | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
| 2226 | // dynamic allocation call could be either of the operands. |
| 2227 | Value *MI = nullptr; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2228 | if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonNullAt(RHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2229 | MI = LHS; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2230 | else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonNullAt(LHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2231 | MI = RHS; |
| 2232 | // FIXME: We should also fold the compare when the pointer escapes, but the |
| 2233 | // compare dominates the pointer escape |
| 2234 | if (MI && !PointerMayBeCaptured(MI, true, true)) |
| 2235 | return ConstantInt::get(GetCompareTy(LHS), |
| 2236 | CmpInst::isFalseWhenEqual(Pred)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2240 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2241 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2242 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2243 | /// Fold an icmp when its operands have i1 scalar type. |
| 2244 | static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS, |
| 2245 | Value *RHS, const Query &Q) { |
| 2246 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2247 | Type *OpTy = LHS->getType(); // The operand type. |
| 2248 | if (!OpTy->getScalarType()->isIntegerTy(1)) |
| 2249 | return nullptr; |
| 2250 | |
| 2251 | switch (Pred) { |
| 2252 | default: |
| 2253 | break; |
| 2254 | case ICmpInst::ICMP_EQ: |
| 2255 | // X == 1 -> X |
| 2256 | if (match(RHS, m_One())) |
| 2257 | return LHS; |
| 2258 | break; |
| 2259 | case ICmpInst::ICMP_NE: |
| 2260 | // X != 0 -> X |
| 2261 | if (match(RHS, m_Zero())) |
| 2262 | return LHS; |
| 2263 | break; |
| 2264 | case ICmpInst::ICMP_UGT: |
| 2265 | // X >u 0 -> X |
| 2266 | if (match(RHS, m_Zero())) |
| 2267 | return LHS; |
| 2268 | break; |
| 2269 | case ICmpInst::ICMP_UGE: |
| 2270 | // X >=u 1 -> X |
| 2271 | if (match(RHS, m_One())) |
| 2272 | return LHS; |
| 2273 | if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false)) |
| 2274 | return getTrue(ITy); |
| 2275 | break; |
| 2276 | case ICmpInst::ICMP_SGE: |
| 2277 | /// For signed comparison, the values for an i1 are 0 and -1 |
| 2278 | /// respectively. This maps into a truth table of: |
| 2279 | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
| 2280 | /// 0 | 0 | 1 (0 >= 0) | 1 |
| 2281 | /// 0 | 1 | 1 (0 >= -1) | 1 |
| 2282 | /// 1 | 0 | 0 (-1 >= 0) | 0 |
| 2283 | /// 1 | 1 | 1 (-1 >= -1) | 1 |
| 2284 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2285 | return getTrue(ITy); |
| 2286 | break; |
| 2287 | case ICmpInst::ICMP_SLT: |
| 2288 | // X <s 0 -> X |
| 2289 | if (match(RHS, m_Zero())) |
| 2290 | return LHS; |
| 2291 | break; |
| 2292 | case ICmpInst::ICMP_SLE: |
| 2293 | // X <=s -1 -> X |
| 2294 | if (match(RHS, m_One())) |
| 2295 | return LHS; |
| 2296 | break; |
| 2297 | case ICmpInst::ICMP_ULE: |
| 2298 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2299 | return getTrue(ITy); |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | return nullptr; |
| 2304 | } |
| 2305 | |
| 2306 | /// Try hard to fold icmp with zero RHS because this is a common case. |
| 2307 | static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS, |
| 2308 | Value *RHS, const Query &Q) { |
| 2309 | if (!match(RHS, m_Zero())) |
| 2310 | return nullptr; |
| 2311 | |
| 2312 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2313 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2314 | switch (Pred) { |
| 2315 | default: |
| 2316 | llvm_unreachable("Unknown ICmp predicate!"); |
| 2317 | case ICmpInst::ICMP_ULT: |
| 2318 | return getFalse(ITy); |
| 2319 | case ICmpInst::ICMP_UGE: |
| 2320 | return getTrue(ITy); |
| 2321 | case ICmpInst::ICMP_EQ: |
| 2322 | case ICmpInst::ICMP_ULE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2323 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2324 | return getFalse(ITy); |
| 2325 | break; |
| 2326 | case ICmpInst::ICMP_NE: |
| 2327 | case ICmpInst::ICMP_UGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2328 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2329 | return getTrue(ITy); |
| 2330 | break; |
| 2331 | case ICmpInst::ICMP_SLT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2332 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2333 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2334 | if (LHSKnownNegative) |
| 2335 | return getTrue(ITy); |
| 2336 | if (LHSKnownNonNegative) |
| 2337 | return getFalse(ITy); |
| 2338 | break; |
| 2339 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2340 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2341 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2342 | if (LHSKnownNegative) |
| 2343 | return getTrue(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2344 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2345 | return getFalse(ITy); |
| 2346 | break; |
| 2347 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2348 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2349 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2350 | if (LHSKnownNegative) |
| 2351 | return getFalse(ITy); |
| 2352 | if (LHSKnownNonNegative) |
| 2353 | return getTrue(ITy); |
| 2354 | break; |
| 2355 | case ICmpInst::ICMP_SGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2356 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2357 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2358 | if (LHSKnownNegative) |
| 2359 | return getFalse(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2360 | if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2361 | return getTrue(ITy); |
| 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | return nullptr; |
| 2366 | } |
| 2367 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2368 | /// Many binary operators with a constant operand have an easy-to-compute |
| 2369 | /// range of outputs. This can be used to fold a comparison to always true or |
| 2370 | /// always false. |
| 2371 | static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper) { |
| 2372 | unsigned Width = Lower.getBitWidth(); |
| 2373 | const APInt *C; |
| 2374 | switch (BO.getOpcode()) { |
| 2375 | case Instruction::Add: |
Sanjay Patel | 5622725 | 2017-01-24 17:03:24 +0000 | [diff] [blame] | 2376 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2377 | // FIXME: If we have both nuw and nsw, we should reduce the range further. |
| 2378 | if (BO.hasNoUnsignedWrap()) { |
| 2379 | // 'add nuw x, C' produces [C, UINT_MAX]. |
| 2380 | Lower = *C; |
| 2381 | } else if (BO.hasNoSignedWrap()) { |
| 2382 | if (C->isNegative()) { |
| 2383 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
| 2384 | Lower = APInt::getSignedMinValue(Width); |
| 2385 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
| 2386 | } else { |
| 2387 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
| 2388 | Lower = APInt::getSignedMinValue(Width) + *C; |
| 2389 | Upper = APInt::getSignedMaxValue(Width) + 1; |
| 2390 | } |
| 2391 | } |
| 2392 | } |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2393 | break; |
| 2394 | |
| 2395 | case Instruction::And: |
| 2396 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2397 | // 'and x, C' produces [0, C]. |
| 2398 | Upper = *C + 1; |
| 2399 | break; |
| 2400 | |
| 2401 | case Instruction::Or: |
| 2402 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2403 | // 'or x, C' produces [C, UINT_MAX]. |
| 2404 | Lower = *C; |
| 2405 | break; |
| 2406 | |
| 2407 | case Instruction::AShr: |
| 2408 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2409 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
| 2410 | Lower = APInt::getSignedMinValue(Width).ashr(*C); |
| 2411 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
| 2412 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2413 | unsigned ShiftAmount = Width - 1; |
| 2414 | if (*C != 0 && BO.isExact()) |
| 2415 | ShiftAmount = C->countTrailingZeros(); |
| 2416 | if (C->isNegative()) { |
| 2417 | // 'ashr C, x' produces [C, C >> (Width-1)] |
| 2418 | Lower = *C; |
| 2419 | Upper = C->ashr(ShiftAmount) + 1; |
| 2420 | } else { |
| 2421 | // 'ashr C, x' produces [C >> (Width-1), C] |
| 2422 | Lower = C->ashr(ShiftAmount); |
| 2423 | Upper = *C + 1; |
| 2424 | } |
| 2425 | } |
| 2426 | break; |
| 2427 | |
| 2428 | case Instruction::LShr: |
| 2429 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2430 | // 'lshr x, C' produces [0, UINT_MAX >> C]. |
| 2431 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; |
| 2432 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2433 | // 'lshr C, x' produces [C >> (Width-1), C]. |
| 2434 | unsigned ShiftAmount = Width - 1; |
| 2435 | if (*C != 0 && BO.isExact()) |
| 2436 | ShiftAmount = C->countTrailingZeros(); |
| 2437 | Lower = C->lshr(ShiftAmount); |
| 2438 | Upper = *C + 1; |
| 2439 | } |
| 2440 | break; |
| 2441 | |
| 2442 | case Instruction::Shl: |
| 2443 | if (match(BO.getOperand(0), m_APInt(C))) { |
| 2444 | if (BO.hasNoUnsignedWrap()) { |
| 2445 | // 'shl nuw C, x' produces [C, C << CLZ(C)] |
| 2446 | Lower = *C; |
| 2447 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
| 2448 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
| 2449 | if (C->isNegative()) { |
| 2450 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] |
| 2451 | unsigned ShiftAmount = C->countLeadingOnes() - 1; |
| 2452 | Lower = C->shl(ShiftAmount); |
| 2453 | Upper = *C + 1; |
| 2454 | } else { |
| 2455 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
| 2456 | unsigned ShiftAmount = C->countLeadingZeros() - 1; |
| 2457 | Lower = *C; |
| 2458 | Upper = C->shl(ShiftAmount) + 1; |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | break; |
| 2463 | |
| 2464 | case Instruction::SDiv: |
| 2465 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2466 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2467 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2468 | if (C->isAllOnesValue()) { |
| 2469 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 2470 | // where C != -1 and C != 0 and C != 1 |
| 2471 | Lower = IntMin + 1; |
| 2472 | Upper = IntMax + 1; |
| 2473 | } else if (C->countLeadingZeros() < Width - 1) { |
| 2474 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
| 2475 | // where C != -1 and C != 0 and C != 1 |
| 2476 | Lower = IntMin.sdiv(*C); |
| 2477 | Upper = IntMax.sdiv(*C); |
| 2478 | if (Lower.sgt(Upper)) |
| 2479 | std::swap(Lower, Upper); |
| 2480 | Upper = Upper + 1; |
| 2481 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 2482 | } |
| 2483 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2484 | if (C->isMinSignedValue()) { |
| 2485 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 2486 | Lower = *C; |
| 2487 | Upper = Lower.lshr(1) + 1; |
| 2488 | } else { |
| 2489 | // 'sdiv C, x' produces [-|C|, |C|]. |
| 2490 | Upper = C->abs() + 1; |
| 2491 | Lower = (-Upper) + 1; |
| 2492 | } |
| 2493 | } |
| 2494 | break; |
| 2495 | |
| 2496 | case Instruction::UDiv: |
| 2497 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2498 | // 'udiv x, C' produces [0, UINT_MAX / C]. |
| 2499 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
| 2500 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2501 | // 'udiv C, x' produces [0, C]. |
| 2502 | Upper = *C + 1; |
| 2503 | } |
| 2504 | break; |
| 2505 | |
| 2506 | case Instruction::SRem: |
| 2507 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2508 | // 'srem x, C' produces (-|C|, |C|). |
| 2509 | Upper = C->abs(); |
| 2510 | Lower = (-Upper) + 1; |
| 2511 | } |
| 2512 | break; |
| 2513 | |
| 2514 | case Instruction::URem: |
| 2515 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2516 | // 'urem x, C' produces [0, C). |
| 2517 | Upper = *C; |
| 2518 | break; |
| 2519 | |
| 2520 | default: |
| 2521 | break; |
| 2522 | } |
| 2523 | } |
| 2524 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2525 | static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, |
| 2526 | Value *RHS) { |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2527 | const APInt *C; |
| 2528 | if (!match(RHS, m_APInt(C))) |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2529 | return nullptr; |
| 2530 | |
| 2531 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
Sanjoy Das | 1f7b813 | 2016-10-02 00:09:57 +0000 | [diff] [blame] | 2532 | ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2533 | if (RHS_CR.isEmptySet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2534 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2535 | if (RHS_CR.isFullSet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2536 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
| 2537 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2538 | // Find the range of possible values for binary operators. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2539 | unsigned Width = C->getBitWidth(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2540 | APInt Lower = APInt(Width, 0); |
| 2541 | APInt Upper = APInt(Width, 0); |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2542 | if (auto *BO = dyn_cast<BinaryOperator>(LHS)) |
| 2543 | setLimitsForBinOp(*BO, Lower, Upper); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2544 | |
| 2545 | ConstantRange LHS_CR = |
| 2546 | Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true); |
| 2547 | |
| 2548 | if (auto *I = dyn_cast<Instruction>(LHS)) |
| 2549 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 2550 | LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges)); |
| 2551 | |
| 2552 | if (!LHS_CR.isFullSet()) { |
| 2553 | if (RHS_CR.contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2554 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2555 | if (RHS_CR.inverse().contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2556 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | return nullptr; |
| 2560 | } |
| 2561 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2562 | static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS, |
| 2563 | Value *RHS, const Query &Q, |
| 2564 | unsigned MaxRecurse) { |
| 2565 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2566 | |
| 2567 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2568 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2569 | if (MaxRecurse && (LBO || RBO)) { |
| 2570 | // Analyze the case when either LHS or RHS is an add instruction. |
| 2571 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
| 2572 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2573 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2574 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2575 | A = LBO->getOperand(0); |
| 2576 | B = LBO->getOperand(1); |
| 2577 | NoLHSWrapProblem = |
| 2578 | ICmpInst::isEquality(Pred) || |
| 2579 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2580 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2581 | } |
| 2582 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2583 | C = RBO->getOperand(0); |
| 2584 | D = RBO->getOperand(1); |
| 2585 | NoRHSWrapProblem = |
| 2586 | ICmpInst::isEquality(Pred) || |
| 2587 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2588 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2589 | } |
| 2590 | |
| 2591 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2592 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2593 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2594 | Constant::getNullValue(RHS->getType()), Q, |
| 2595 | MaxRecurse - 1)) |
| 2596 | return V; |
| 2597 | |
| 2598 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2599 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2600 | if (Value *V = |
| 2601 | SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()), |
| 2602 | C == LHS ? D : C, Q, MaxRecurse - 1)) |
| 2603 | return V; |
| 2604 | |
| 2605 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2606 | if (A && C && (A == C || A == D || B == C || B == D) && NoLHSWrapProblem && |
| 2607 | NoRHSWrapProblem) { |
| 2608 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2609 | Value *Y, *Z; |
| 2610 | if (A == C) { |
| 2611 | // C + B == C + D -> B == D |
| 2612 | Y = B; |
| 2613 | Z = D; |
| 2614 | } else if (A == D) { |
| 2615 | // D + B == C + D -> B == C |
| 2616 | Y = B; |
| 2617 | Z = C; |
| 2618 | } else if (B == C) { |
| 2619 | // A + C == C + D -> A == D |
| 2620 | Y = A; |
| 2621 | Z = D; |
| 2622 | } else { |
| 2623 | assert(B == D); |
| 2624 | // A + D == C + D -> A == C |
| 2625 | Y = A; |
| 2626 | Z = C; |
| 2627 | } |
| 2628 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1)) |
| 2629 | return V; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | { |
| 2634 | Value *Y = nullptr; |
| 2635 | // icmp pred (or X, Y), X |
| 2636 | if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
| 2637 | if (Pred == ICmpInst::ICMP_ULT) |
| 2638 | return getFalse(ITy); |
| 2639 | if (Pred == ICmpInst::ICMP_UGE) |
| 2640 | return getTrue(ITy); |
| 2641 | |
| 2642 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { |
| 2643 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 2644 | bool YKnownNonNegative, YKnownNegative; |
| 2645 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2646 | Q.AC, Q.CxtI, Q.DT); |
| 2647 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2648 | Q.CxtI, Q.DT); |
| 2649 | if (RHSKnownNonNegative && YKnownNegative) |
| 2650 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
| 2651 | if (RHSKnownNegative || YKnownNonNegative) |
| 2652 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy); |
| 2653 | } |
| 2654 | } |
| 2655 | // icmp pred X, (or X, Y) |
| 2656 | if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) { |
| 2657 | if (Pred == ICmpInst::ICMP_ULE) |
| 2658 | return getTrue(ITy); |
| 2659 | if (Pred == ICmpInst::ICMP_UGT) |
| 2660 | return getFalse(ITy); |
| 2661 | |
| 2662 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) { |
| 2663 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2664 | bool YKnownNonNegative, YKnownNegative; |
| 2665 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2666 | Q.AC, Q.CxtI, Q.DT); |
| 2667 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2668 | Q.CxtI, Q.DT); |
| 2669 | if (LHSKnownNonNegative && YKnownNegative) |
| 2670 | return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy); |
| 2671 | if (LHSKnownNegative || YKnownNonNegative) |
| 2672 | return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy); |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
| 2677 | // icmp pred (and X, Y), X |
| 2678 | if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)), |
| 2679 | m_And(m_Specific(RHS), m_Value())))) { |
| 2680 | if (Pred == ICmpInst::ICMP_UGT) |
| 2681 | return getFalse(ITy); |
| 2682 | if (Pred == ICmpInst::ICMP_ULE) |
| 2683 | return getTrue(ITy); |
| 2684 | } |
| 2685 | // icmp pred X, (and X, Y) |
| 2686 | if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)), |
| 2687 | m_And(m_Specific(LHS), m_Value())))) { |
| 2688 | if (Pred == ICmpInst::ICMP_UGE) |
| 2689 | return getTrue(ITy); |
| 2690 | if (Pred == ICmpInst::ICMP_ULT) |
| 2691 | return getFalse(ITy); |
| 2692 | } |
| 2693 | |
| 2694 | // 0 - (zext X) pred C |
| 2695 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2696 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2697 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2698 | if (Pred == ICmpInst::ICMP_SLT) |
| 2699 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2700 | if (Pred == ICmpInst::ICMP_SGE) |
| 2701 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2702 | if (Pred == ICmpInst::ICMP_EQ) |
| 2703 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2704 | if (Pred == ICmpInst::ICMP_NE) |
| 2705 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2706 | } |
| 2707 | if (RHSC->getValue().isNonNegative()) { |
| 2708 | if (Pred == ICmpInst::ICMP_SLE) |
| 2709 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2710 | if (Pred == ICmpInst::ICMP_SGT) |
| 2711 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | // icmp pred (urem X, Y), Y |
| 2717 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
| 2718 | bool KnownNonNegative, KnownNegative; |
| 2719 | switch (Pred) { |
| 2720 | default: |
| 2721 | break; |
| 2722 | case ICmpInst::ICMP_SGT: |
| 2723 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2724 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2725 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2726 | if (!KnownNonNegative) |
| 2727 | break; |
| 2728 | LLVM_FALLTHROUGH; |
| 2729 | case ICmpInst::ICMP_EQ: |
| 2730 | case ICmpInst::ICMP_UGT: |
| 2731 | case ICmpInst::ICMP_UGE: |
| 2732 | return getFalse(ITy); |
| 2733 | case ICmpInst::ICMP_SLT: |
| 2734 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2735 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2736 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2737 | if (!KnownNonNegative) |
| 2738 | break; |
| 2739 | LLVM_FALLTHROUGH; |
| 2740 | case ICmpInst::ICMP_NE: |
| 2741 | case ICmpInst::ICMP_ULT: |
| 2742 | case ICmpInst::ICMP_ULE: |
| 2743 | return getTrue(ITy); |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | // icmp pred X, (urem Y, X) |
| 2748 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2749 | bool KnownNonNegative, KnownNegative; |
| 2750 | switch (Pred) { |
| 2751 | default: |
| 2752 | break; |
| 2753 | case ICmpInst::ICMP_SGT: |
| 2754 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2755 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2756 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2757 | if (!KnownNonNegative) |
| 2758 | break; |
| 2759 | LLVM_FALLTHROUGH; |
| 2760 | case ICmpInst::ICMP_NE: |
| 2761 | case ICmpInst::ICMP_UGT: |
| 2762 | case ICmpInst::ICMP_UGE: |
| 2763 | return getTrue(ITy); |
| 2764 | case ICmpInst::ICMP_SLT: |
| 2765 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2766 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2767 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2768 | if (!KnownNonNegative) |
| 2769 | break; |
| 2770 | LLVM_FALLTHROUGH; |
| 2771 | case ICmpInst::ICMP_EQ: |
| 2772 | case ICmpInst::ICMP_ULT: |
| 2773 | case ICmpInst::ICMP_ULE: |
| 2774 | return getFalse(ITy); |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | // x >> y <=u x |
| 2779 | // x udiv y <=u x. |
| 2780 | if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) || |
| 2781 | match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) { |
| 2782 | // icmp pred (X op Y), X |
| 2783 | if (Pred == ICmpInst::ICMP_UGT) |
| 2784 | return getFalse(ITy); |
| 2785 | if (Pred == ICmpInst::ICMP_ULE) |
| 2786 | return getTrue(ITy); |
| 2787 | } |
| 2788 | |
| 2789 | // x >=u x >> y |
| 2790 | // x >=u x udiv y. |
| 2791 | if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) || |
| 2792 | match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) { |
| 2793 | // icmp pred X, (X op Y) |
| 2794 | if (Pred == ICmpInst::ICMP_ULT) |
| 2795 | return getFalse(ITy); |
| 2796 | if (Pred == ICmpInst::ICMP_UGE) |
| 2797 | return getTrue(ITy); |
| 2798 | } |
| 2799 | |
| 2800 | // handle: |
| 2801 | // CI2 << X == CI |
| 2802 | // CI2 << X != CI |
| 2803 | // |
| 2804 | // where CI2 is a power of 2 and CI isn't |
| 2805 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2806 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2807 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2808 | CI2Val->isPowerOf2()) { |
| 2809 | if (!CIVal->isPowerOf2()) { |
| 2810 | // CI2 << X can equal zero in some circumstances, |
| 2811 | // this simplification is unsafe if CI is zero. |
| 2812 | // |
| 2813 | // We know it is safe if: |
| 2814 | // - The shift is nsw, we can't shift out the one bit. |
| 2815 | // - The shift is nuw, we can't shift out the one bit. |
| 2816 | // - CI2 is one |
| 2817 | // - CI isn't zero |
| 2818 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
| 2819 | *CI2Val == 1 || !CI->isZero()) { |
| 2820 | if (Pred == ICmpInst::ICMP_EQ) |
| 2821 | return ConstantInt::getFalse(RHS->getContext()); |
| 2822 | if (Pred == ICmpInst::ICMP_NE) |
| 2823 | return ConstantInt::getTrue(RHS->getContext()); |
| 2824 | } |
| 2825 | } |
| 2826 | if (CIVal->isSignBit() && *CI2Val == 1) { |
| 2827 | if (Pred == ICmpInst::ICMP_UGT) |
| 2828 | return ConstantInt::getFalse(RHS->getContext()); |
| 2829 | if (Pred == ICmpInst::ICMP_ULE) |
| 2830 | return ConstantInt::getTrue(RHS->getContext()); |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2836 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2837 | switch (LBO->getOpcode()) { |
| 2838 | default: |
| 2839 | break; |
| 2840 | case Instruction::UDiv: |
| 2841 | case Instruction::LShr: |
| 2842 | if (ICmpInst::isSigned(Pred)) |
| 2843 | break; |
| 2844 | LLVM_FALLTHROUGH; |
| 2845 | case Instruction::SDiv: |
| 2846 | case Instruction::AShr: |
| 2847 | if (!LBO->isExact() || !RBO->isExact()) |
| 2848 | break; |
| 2849 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2850 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2851 | return V; |
| 2852 | break; |
| 2853 | case Instruction::Shl: { |
| 2854 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
| 2855 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2856 | if (!NUW && !NSW) |
| 2857 | break; |
| 2858 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2859 | break; |
| 2860 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2861 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2862 | return V; |
| 2863 | break; |
| 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | return nullptr; |
| 2868 | } |
| 2869 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 2870 | /// Simplify integer comparisons where at least one operand of the compare |
| 2871 | /// matches an integer min/max idiom. |
| 2872 | static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, |
| 2873 | Value *RHS, const Query &Q, |
| 2874 | unsigned MaxRecurse) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2875 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2876 | Value *A, *B; |
| 2877 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2878 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2879 | |
| 2880 | // Signed variants on "max(a,b)>=a -> true". |
| 2881 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2882 | if (A != RHS) |
| 2883 | std::swap(A, B); // smax(A, B) pred A. |
| 2884 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2885 | // We analyze this as smax(A, B) pred A. |
| 2886 | P = Pred; |
| 2887 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2888 | (A == LHS || B == LHS)) { |
| 2889 | if (A != LHS) |
| 2890 | std::swap(A, B); // A pred smax(A, B). |
| 2891 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2892 | // We analyze this as smax(A, B) swapped-pred A. |
| 2893 | P = CmpInst::getSwappedPredicate(Pred); |
| 2894 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2895 | (A == RHS || B == RHS)) { |
| 2896 | if (A != RHS) |
| 2897 | std::swap(A, B); // smin(A, B) pred A. |
| 2898 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2899 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2900 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2901 | P = CmpInst::getSwappedPredicate(Pred); |
| 2902 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2903 | (A == LHS || B == LHS)) { |
| 2904 | if (A != LHS) |
| 2905 | std::swap(A, B); // A pred smin(A, B). |
| 2906 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2907 | // We analyze this as smax(-A, -B) pred -A. |
| 2908 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2909 | P = Pred; |
| 2910 | } |
| 2911 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2912 | // Cases correspond to "max(A, B) p A". |
| 2913 | switch (P) { |
| 2914 | default: |
| 2915 | break; |
| 2916 | case CmpInst::ICMP_EQ: |
| 2917 | case CmpInst::ICMP_SLE: |
| 2918 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2919 | // in the max/min; if so, we can just return that. |
| 2920 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2921 | return V; |
| 2922 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2923 | return V; |
| 2924 | // Otherwise, see if "A EqP B" simplifies. |
| 2925 | if (MaxRecurse) |
| 2926 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 2927 | return V; |
| 2928 | break; |
| 2929 | case CmpInst::ICMP_NE: |
| 2930 | case CmpInst::ICMP_SGT: { |
| 2931 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2932 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2933 | // tested in the max/min; if so, we can just return that. |
| 2934 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2935 | return V; |
| 2936 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2937 | return V; |
| 2938 | // Otherwise, see if "A InvEqP B" simplifies. |
| 2939 | if (MaxRecurse) |
| 2940 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 2941 | return V; |
| 2942 | break; |
| 2943 | } |
| 2944 | case CmpInst::ICMP_SGE: |
| 2945 | // Always true. |
| 2946 | return getTrue(ITy); |
| 2947 | case CmpInst::ICMP_SLT: |
| 2948 | // Always false. |
| 2949 | return getFalse(ITy); |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | // Unsigned variants on "max(a,b)>=a -> true". |
| 2954 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2955 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2956 | if (A != RHS) |
| 2957 | std::swap(A, B); // umax(A, B) pred A. |
| 2958 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2959 | // We analyze this as umax(A, B) pred A. |
| 2960 | P = Pred; |
| 2961 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2962 | (A == LHS || B == LHS)) { |
| 2963 | if (A != LHS) |
| 2964 | std::swap(A, B); // A pred umax(A, B). |
| 2965 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2966 | // We analyze this as umax(A, B) swapped-pred A. |
| 2967 | P = CmpInst::getSwappedPredicate(Pred); |
| 2968 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2969 | (A == RHS || B == RHS)) { |
| 2970 | if (A != RHS) |
| 2971 | std::swap(A, B); // umin(A, B) pred A. |
| 2972 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2973 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2974 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2975 | P = CmpInst::getSwappedPredicate(Pred); |
| 2976 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2977 | (A == LHS || B == LHS)) { |
| 2978 | if (A != LHS) |
| 2979 | std::swap(A, B); // A pred umin(A, B). |
| 2980 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2981 | // We analyze this as umax(-A, -B) pred -A. |
| 2982 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2983 | P = Pred; |
| 2984 | } |
| 2985 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2986 | // Cases correspond to "max(A, B) p A". |
| 2987 | switch (P) { |
| 2988 | default: |
| 2989 | break; |
| 2990 | case CmpInst::ICMP_EQ: |
| 2991 | case CmpInst::ICMP_ULE: |
| 2992 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2993 | // in the max/min; if so, we can just return that. |
| 2994 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2995 | return V; |
| 2996 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2997 | return V; |
| 2998 | // Otherwise, see if "A EqP B" simplifies. |
| 2999 | if (MaxRecurse) |
| 3000 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 3001 | return V; |
| 3002 | break; |
| 3003 | case CmpInst::ICMP_NE: |
| 3004 | case CmpInst::ICMP_UGT: { |
| 3005 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 3006 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 3007 | // tested in the max/min; if so, we can just return that. |
| 3008 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 3009 | return V; |
| 3010 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 3011 | return V; |
| 3012 | // Otherwise, see if "A InvEqP B" simplifies. |
| 3013 | if (MaxRecurse) |
| 3014 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 3015 | return V; |
| 3016 | break; |
| 3017 | } |
| 3018 | case CmpInst::ICMP_UGE: |
| 3019 | // Always true. |
| 3020 | return getTrue(ITy); |
| 3021 | case CmpInst::ICMP_ULT: |
| 3022 | // Always false. |
| 3023 | return getFalse(ITy); |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | // Variants on "max(x,y) >= min(x,z)". |
| 3028 | Value *C, *D; |
| 3029 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 3030 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 3031 | (A == C || A == D || B == C || B == D)) { |
| 3032 | // max(x, ?) pred min(x, ?). |
| 3033 | if (Pred == CmpInst::ICMP_SGE) |
| 3034 | // Always true. |
| 3035 | return getTrue(ITy); |
| 3036 | if (Pred == CmpInst::ICMP_SLT) |
| 3037 | // Always false. |
| 3038 | return getFalse(ITy); |
| 3039 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 3040 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 3041 | (A == C || A == D || B == C || B == D)) { |
| 3042 | // min(x, ?) pred max(x, ?). |
| 3043 | if (Pred == CmpInst::ICMP_SLE) |
| 3044 | // Always true. |
| 3045 | return getTrue(ITy); |
| 3046 | if (Pred == CmpInst::ICMP_SGT) |
| 3047 | // Always false. |
| 3048 | return getFalse(ITy); |
| 3049 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 3050 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 3051 | (A == C || A == D || B == C || B == D)) { |
| 3052 | // max(x, ?) pred min(x, ?). |
| 3053 | if (Pred == CmpInst::ICMP_UGE) |
| 3054 | // Always true. |
| 3055 | return getTrue(ITy); |
| 3056 | if (Pred == CmpInst::ICMP_ULT) |
| 3057 | // Always false. |
| 3058 | return getFalse(ITy); |
| 3059 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3060 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 3061 | (A == C || A == D || B == C || B == D)) { |
| 3062 | // min(x, ?) pred max(x, ?). |
| 3063 | if (Pred == CmpInst::ICMP_ULE) |
| 3064 | // Always true. |
| 3065 | return getTrue(ITy); |
| 3066 | if (Pred == CmpInst::ICMP_UGT) |
| 3067 | // Always false. |
| 3068 | return getFalse(ITy); |
| 3069 | } |
| 3070 | |
| 3071 | return nullptr; |
| 3072 | } |
| 3073 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3074 | /// Given operands for an ICmpInst, see if we can fold the result. |
| 3075 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3076 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3077 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3078 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3079 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3081 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3082 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3083 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3084 | |
| 3085 | // If we have a constant, make sure it is on the RHS. |
| 3086 | std::swap(LHS, RHS); |
| 3087 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3088 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3090 | Type *ITy = GetCompareTy(LHS); // The return type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3092 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 3093 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 3094 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3095 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3096 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3097 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3098 | if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) |
| 3099 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3100 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3101 | if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) |
| 3102 | return V; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 3103 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 3104 | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS)) |
| 3105 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3106 | |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3107 | // If both operands have range metadata, use the metadata |
| 3108 | // to simplify the comparison. |
| 3109 | if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) { |
Craig Topper | 0c19861 | 2017-04-10 19:37:10 +0000 | [diff] [blame] | 3110 | auto RHS_Instr = cast<Instruction>(RHS); |
| 3111 | auto LHS_Instr = cast<Instruction>(LHS); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3112 | |
| 3113 | if (RHS_Instr->getMetadata(LLVMContext::MD_range) && |
| 3114 | LHS_Instr->getMetadata(LLVMContext::MD_range)) { |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 3115 | auto RHS_CR = getConstantRangeFromMetadata( |
| 3116 | *RHS_Instr->getMetadata(LLVMContext::MD_range)); |
| 3117 | auto LHS_CR = getConstantRangeFromMetadata( |
| 3118 | *LHS_Instr->getMetadata(LLVMContext::MD_range)); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3119 | |
| 3120 | auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); |
| 3121 | if (Satisfied_CR.contains(LHS_CR)) |
| 3122 | return ConstantInt::getTrue(RHS->getContext()); |
| 3123 | |
| 3124 | auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( |
| 3125 | CmpInst::getInversePredicate(Pred), RHS_CR); |
| 3126 | if (InversedSatisfied_CR.contains(LHS_CR)) |
| 3127 | return ConstantInt::getFalse(RHS->getContext()); |
| 3128 | } |
| 3129 | } |
| 3130 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3131 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 3132 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 3133 | Instruction *LI = cast<CastInst>(LHS); |
| 3134 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3135 | Type *SrcTy = SrcOp->getType(); |
| 3136 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3137 | |
| 3138 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 3139 | // if the integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3140 | if (MaxRecurse && isa<PtrToIntInst>(LI) && |
| 3141 | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3142 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 3143 | // Transfer the cast to the constant. |
| 3144 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 3145 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3146 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3147 | return V; |
| 3148 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 3149 | if (RI->getOperand(0)->getType() == SrcTy) |
| 3150 | // Compare without the cast. |
| 3151 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3152 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3153 | return V; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | if (isa<ZExtInst>(LHS)) { |
| 3158 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 3159 | // same type. |
| 3160 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 3161 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3162 | // Compare X and Y. Note that signed predicates become unsigned. |
| 3163 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3164 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3165 | MaxRecurse-1)) |
| 3166 | return V; |
| 3167 | } |
| 3168 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 3169 | // too. If not, then try to deduce the result of the comparison. |
| 3170 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3171 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3172 | // reextended to DstTy. |
| 3173 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3174 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 3175 | |
| 3176 | // If the re-extended constant didn't change then this is effectively |
| 3177 | // also a case of comparing two zero-extended values. |
| 3178 | if (RExt == CI && MaxRecurse) |
| 3179 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3180 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3181 | return V; |
| 3182 | |
| 3183 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 3184 | // there. Use this to work out the result of the comparison. |
| 3185 | if (RExt != CI) { |
| 3186 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3187 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3188 | // LHS <u RHS. |
| 3189 | case ICmpInst::ICMP_EQ: |
| 3190 | case ICmpInst::ICMP_UGT: |
| 3191 | case ICmpInst::ICMP_UGE: |
| 3192 | return ConstantInt::getFalse(CI->getContext()); |
| 3193 | |
| 3194 | case ICmpInst::ICMP_NE: |
| 3195 | case ICmpInst::ICMP_ULT: |
| 3196 | case ICmpInst::ICMP_ULE: |
| 3197 | return ConstantInt::getTrue(CI->getContext()); |
| 3198 | |
| 3199 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 3200 | // is non-negative then LHS <s RHS. |
| 3201 | case ICmpInst::ICMP_SGT: |
| 3202 | case ICmpInst::ICMP_SGE: |
| 3203 | return CI->getValue().isNegative() ? |
| 3204 | ConstantInt::getTrue(CI->getContext()) : |
| 3205 | ConstantInt::getFalse(CI->getContext()); |
| 3206 | |
| 3207 | case ICmpInst::ICMP_SLT: |
| 3208 | case ICmpInst::ICMP_SLE: |
| 3209 | return CI->getValue().isNegative() ? |
| 3210 | ConstantInt::getFalse(CI->getContext()) : |
| 3211 | ConstantInt::getTrue(CI->getContext()); |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | if (isa<SExtInst>(LHS)) { |
| 3218 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 3219 | // same type. |
| 3220 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 3221 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3222 | // Compare X and Y. Note that the predicate does not change. |
| 3223 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3224 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3225 | return V; |
| 3226 | } |
| 3227 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 3228 | // too. If not, then try to deduce the result of the comparison. |
| 3229 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3230 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3231 | // reextended to DstTy. |
| 3232 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3233 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 3234 | |
| 3235 | // If the re-extended constant didn't change then this is effectively |
| 3236 | // also a case of comparing two sign-extended values. |
| 3237 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3238 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3239 | return V; |
| 3240 | |
| 3241 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 3242 | // bits there. Use this to work out the result of the comparison. |
| 3243 | if (RExt != CI) { |
| 3244 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3245 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3246 | case ICmpInst::ICMP_EQ: |
| 3247 | return ConstantInt::getFalse(CI->getContext()); |
| 3248 | case ICmpInst::ICMP_NE: |
| 3249 | return ConstantInt::getTrue(CI->getContext()); |
| 3250 | |
| 3251 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 3252 | // LHS >s RHS. |
| 3253 | case ICmpInst::ICMP_SGT: |
| 3254 | case ICmpInst::ICMP_SGE: |
| 3255 | return CI->getValue().isNegative() ? |
| 3256 | ConstantInt::getTrue(CI->getContext()) : |
| 3257 | ConstantInt::getFalse(CI->getContext()); |
| 3258 | case ICmpInst::ICMP_SLT: |
| 3259 | case ICmpInst::ICMP_SLE: |
| 3260 | return CI->getValue().isNegative() ? |
| 3261 | ConstantInt::getFalse(CI->getContext()) : |
| 3262 | ConstantInt::getTrue(CI->getContext()); |
| 3263 | |
| 3264 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 3265 | // LHS >u RHS. |
| 3266 | case ICmpInst::ICMP_UGT: |
| 3267 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3268 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3269 | if (MaxRecurse) |
| 3270 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 3271 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3272 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3273 | return V; |
| 3274 | break; |
| 3275 | case ICmpInst::ICMP_ULT: |
| 3276 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3277 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3278 | if (MaxRecurse) |
| 3279 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 3280 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3281 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3282 | return V; |
| 3283 | break; |
| 3284 | } |
| 3285 | } |
| 3286 | } |
| 3287 | } |
| 3288 | } |
| 3289 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3290 | // icmp eq|ne X, Y -> false|true if X != Y |
| 3291 | if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3292 | isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) { |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3293 | LLVMContext &Ctx = LHS->getType()->getContext(); |
| 3294 | return Pred == ICmpInst::ICMP_NE ? |
| 3295 | ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx); |
| 3296 | } |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 3297 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3298 | if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse)) |
| 3299 | return V; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 3300 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 3301 | if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3302 | return V; |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3303 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3304 | // Simplify comparisons of related pointers using a powerful, recursive |
| 3305 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 3306 | if (LHS->getType()->isPointerTy()) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 3307 | 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] | 3308 | return C; |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3309 | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
| 3310 | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
| 3311 | if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
| 3312 | Q.DL.getTypeSizeInBits(CLHS->getType()) && |
| 3313 | Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) == |
| 3314 | Q.DL.getTypeSizeInBits(CRHS->getType())) |
| 3315 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, |
| 3316 | CLHS->getPointerOperand(), |
| 3317 | CRHS->getPointerOperand())) |
| 3318 | return C; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3319 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3320 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 3321 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 3322 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 3323 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 3324 | (ICmpInst::isEquality(Pred) || |
| 3325 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 3326 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 3327 | // The bases are equal and the indices are constant. Build a constant |
| 3328 | // expression GEP with the same indices and a null base pointer to see |
| 3329 | // what constant folding can make out of it. |
| 3330 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 3331 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3332 | Constant *NewLHS = ConstantExpr::getGetElementPtr( |
| 3333 | GLHS->getSourceElementType(), Null, IndicesLHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3334 | |
| 3335 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3336 | Constant *NewRHS = ConstantExpr::getGetElementPtr( |
| 3337 | GLHS->getSourceElementType(), Null, IndicesRHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3338 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 3339 | } |
| 3340 | } |
| 3341 | } |
| 3342 | |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3343 | // If a bit is known to be zero for A and known to be one for B, |
| 3344 | // then A and B cannot be equal. |
| 3345 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3346 | const APInt *RHSVal; |
| 3347 | if (match(RHS, m_APInt(RHSVal))) { |
| 3348 | unsigned BitWidth = RHSVal->getBitWidth(); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3349 | APInt LHSKnownZero(BitWidth, 0); |
| 3350 | APInt LHSKnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3351 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC, |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3352 | Q.CxtI, Q.DT); |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3353 | if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0)) |
| 3354 | return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy) |
| 3355 | : ConstantInt::getTrue(ITy); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3359 | // If the comparison is with the result of a select instruction, check whether |
| 3360 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3361 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3362 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3363 | return V; |
| 3364 | |
| 3365 | // If the comparison is with the result of a phi instruction, check whether |
| 3366 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3367 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3368 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3369 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3370 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3371 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3374 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3375 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3376 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3377 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 85dbea9 | 2015-12-24 09:08:08 +0000 | [diff] [blame] | 3378 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3379 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3380 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3383 | /// Given operands for an FCmpInst, see if we can fold the result. |
| 3384 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3385 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3386 | FastMathFlags FMF, const Query &Q, |
| 3387 | unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3388 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 3389 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 3390 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3391 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3392 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3393 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3394 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3395 | // If we have a constant, make sure it is on the RHS. |
| 3396 | std::swap(LHS, RHS); |
| 3397 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3398 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3399 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3400 | // Fold trivial predicates. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3401 | Type *RetTy = GetCompareTy(LHS); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3402 | if (Pred == FCmpInst::FCMP_FALSE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3403 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3404 | if (Pred == FCmpInst::FCMP_TRUE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3405 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3406 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3407 | // UNO/ORD predicates can be trivially folded if NaNs are ignored. |
| 3408 | if (FMF.noNaNs()) { |
| 3409 | if (Pred == FCmpInst::FCMP_UNO) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3410 | return getFalse(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3411 | if (Pred == FCmpInst::FCMP_ORD) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3412 | return getTrue(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3415 | // fcmp pred x, undef and fcmp pred undef, x |
| 3416 | // fold to true if unordered, false if ordered |
| 3417 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) { |
| 3418 | // Choosing NaN for the undef will always make unordered comparison succeed |
| 3419 | // and ordered comparison fail. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3420 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3421 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3422 | |
| 3423 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3424 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3425 | if (CmpInst::isTrueWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3426 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3427 | if (CmpInst::isFalseWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3428 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3429 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3430 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3431 | // Handle fcmp with constant RHS |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3432 | const ConstantFP *CFP = nullptr; |
| 3433 | if (const auto *RHSC = dyn_cast<Constant>(RHS)) { |
| 3434 | if (RHS->getType()->isVectorTy()) |
| 3435 | CFP = dyn_cast_or_null<ConstantFP>(RHSC->getSplatValue()); |
| 3436 | else |
| 3437 | CFP = dyn_cast<ConstantFP>(RHSC); |
| 3438 | } |
| 3439 | if (CFP) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3440 | // 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] | 3441 | if (CFP->getValueAPF().isNaN()) { |
| 3442 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3443 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3444 | assert(FCmpInst::isUnordered(Pred) && |
| 3445 | "Comparison must be either ordered or unordered!"); |
| 3446 | // True if unordered. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3447 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3448 | } |
| 3449 | // Check whether the constant is an infinity. |
| 3450 | if (CFP->getValueAPF().isInfinity()) { |
| 3451 | if (CFP->getValueAPF().isNegative()) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3452 | switch (Pred) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3453 | case FCmpInst::FCMP_OLT: |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3454 | // No value is ordered and less than negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3455 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3456 | case FCmpInst::FCMP_UGE: |
| 3457 | // All values are unordered with or at least negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3458 | return getTrue(RetTy); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3459 | default: |
| 3460 | break; |
| 3461 | } |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3462 | } else { |
| 3463 | switch (Pred) { |
| 3464 | case FCmpInst::FCMP_OGT: |
| 3465 | // No value is ordered and greater than infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3466 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3467 | case FCmpInst::FCMP_ULE: |
| 3468 | // All values are unordered with and at most infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3469 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3470 | default: |
| 3471 | break; |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | if (CFP->getValueAPF().isZero()) { |
| 3476 | switch (Pred) { |
| 3477 | case FCmpInst::FCMP_UGE: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3478 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3479 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3480 | break; |
| 3481 | case FCmpInst::FCMP_OLT: |
| 3482 | // X < 0 |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3483 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3484 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3485 | break; |
| 3486 | default: |
| 3487 | break; |
| 3488 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3489 | } |
| 3490 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3491 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3492 | // If the comparison is with the result of a select instruction, check whether |
| 3493 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3494 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3495 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3496 | return V; |
| 3497 | |
| 3498 | // If the comparison is with the result of a phi instruction, check whether |
| 3499 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3500 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3501 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3502 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3503 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3504 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3507 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3508 | FastMathFlags FMF, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3509 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3510 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3511 | const Instruction *CxtI) { |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3512 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3513 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3516 | /// See if V simplifies when its operand Op is replaced with RepOp. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3517 | static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
| 3518 | const Query &Q, |
| 3519 | unsigned MaxRecurse) { |
| 3520 | // Trivial replacement. |
| 3521 | if (V == Op) |
| 3522 | return RepOp; |
| 3523 | |
| 3524 | auto *I = dyn_cast<Instruction>(V); |
| 3525 | if (!I) |
| 3526 | return nullptr; |
| 3527 | |
| 3528 | // If this is a binary operator, try to simplify it with the replaced op. |
| 3529 | if (auto *B = dyn_cast<BinaryOperator>(I)) { |
| 3530 | // Consider: |
| 3531 | // %cmp = icmp eq i32 %x, 2147483647 |
| 3532 | // %add = add nsw i32 %x, 1 |
| 3533 | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
| 3534 | // |
| 3535 | // We can't replace %sel with %add unless we strip away the flags. |
| 3536 | if (isa<OverflowingBinaryOperator>(B)) |
| 3537 | if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap()) |
| 3538 | return nullptr; |
| 3539 | if (isa<PossiblyExactOperator>(B)) |
| 3540 | if (B->isExact()) |
| 3541 | return nullptr; |
| 3542 | |
| 3543 | if (MaxRecurse) { |
| 3544 | if (B->getOperand(0) == Op) |
| 3545 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q, |
| 3546 | MaxRecurse - 1); |
| 3547 | if (B->getOperand(1) == Op) |
| 3548 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q, |
| 3549 | MaxRecurse - 1); |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | // Same for CmpInsts. |
| 3554 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 3555 | if (MaxRecurse) { |
| 3556 | if (C->getOperand(0) == Op) |
| 3557 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q, |
| 3558 | MaxRecurse - 1); |
| 3559 | if (C->getOperand(1) == Op) |
| 3560 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q, |
| 3561 | MaxRecurse - 1); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | // TODO: We could hand off more cases to instsimplify here. |
| 3566 | |
| 3567 | // If all operands are constant after substituting Op for RepOp then we can |
| 3568 | // constant fold the instruction. |
| 3569 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 3570 | // Build a list of all constant operands. |
| 3571 | SmallVector<Constant *, 8> ConstOps; |
| 3572 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3573 | if (I->getOperand(i) == Op) |
| 3574 | ConstOps.push_back(CRepOp); |
| 3575 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 3576 | ConstOps.push_back(COp); |
| 3577 | else |
| 3578 | break; |
| 3579 | } |
| 3580 | |
| 3581 | // All operands were constants, fold it. |
| 3582 | if (ConstOps.size() == I->getNumOperands()) { |
| 3583 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 3584 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 3585 | ConstOps[1], Q.DL, Q.TLI); |
| 3586 | |
| 3587 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 3588 | if (!LI->isVolatile()) |
Eduard Burtescu | 1423921 | 2016-01-22 01:17:26 +0000 | [diff] [blame] | 3589 | return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3590 | |
Manuel Jacob | e902459 | 2016-01-21 06:33:22 +0000 | [diff] [blame] | 3591 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | return nullptr; |
| 3596 | } |
| 3597 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3598 | /// Try to simplify a select instruction when its condition operand is an |
| 3599 | /// integer comparison where one operand of the compare is a constant. |
| 3600 | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
| 3601 | const APInt *Y, bool TrueWhenUnset) { |
| 3602 | const APInt *C; |
| 3603 | |
| 3604 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3605 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3606 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3607 | *Y == ~*C) |
| 3608 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3609 | |
| 3610 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3611 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3612 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3613 | *Y == ~*C) |
| 3614 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3615 | |
| 3616 | if (Y->isPowerOf2()) { |
| 3617 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3618 | // (X & Y) != 0 ? X | Y : X --> X |
| 3619 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3620 | *Y == *C) |
| 3621 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3622 | |
| 3623 | // (X & Y) == 0 ? X : X | Y --> X |
| 3624 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3625 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3626 | *Y == *C) |
| 3627 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3628 | } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3629 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3630 | return nullptr; |
| 3631 | } |
| 3632 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3633 | /// An alternative way to test if a bit is set or not uses sgt/slt instead of |
| 3634 | /// eq/ne. |
| 3635 | static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *TrueVal, |
| 3636 | Value *FalseVal, |
| 3637 | bool TrueWhenUnset) { |
| 3638 | unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); |
Sanjay Patel | e9fc79b | 2016-07-21 21:56:00 +0000 | [diff] [blame] | 3639 | if (!BitWidth) |
| 3640 | return nullptr; |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3641 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3642 | APInt MinSignedValue; |
| 3643 | Value *X; |
| 3644 | if (match(CmpLHS, m_Trunc(m_Value(X))) && (X == TrueVal || X == FalseVal)) { |
| 3645 | // icmp slt (trunc X), 0 <--> icmp ne (and X, C), 0 |
| 3646 | // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0 |
| 3647 | unsigned DestSize = CmpLHS->getType()->getScalarSizeInBits(); |
| 3648 | MinSignedValue = APInt::getSignedMinValue(DestSize).zext(BitWidth); |
| 3649 | } else { |
| 3650 | // icmp slt X, 0 <--> icmp ne (and X, C), 0 |
| 3651 | // icmp sgt X, -1 <--> icmp eq (and X, C), 0 |
| 3652 | X = CmpLHS; |
| 3653 | MinSignedValue = APInt::getSignedMinValue(BitWidth); |
| 3654 | } |
| 3655 | |
| 3656 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, &MinSignedValue, |
| 3657 | TrueWhenUnset)) |
| 3658 | return V; |
| 3659 | |
| 3660 | return nullptr; |
| 3661 | } |
| 3662 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3663 | /// Try to simplify a select instruction when its condition operand is an |
| 3664 | /// integer comparison. |
| 3665 | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
| 3666 | Value *FalseVal, const Query &Q, |
| 3667 | unsigned MaxRecurse) { |
| 3668 | ICmpInst::Predicate Pred; |
| 3669 | Value *CmpLHS, *CmpRHS; |
| 3670 | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
| 3671 | return nullptr; |
| 3672 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 3673 | // FIXME: This code is nearly duplicated in InstCombine. Using/refactoring |
| 3674 | // decomposeBitTestICmp() might help. |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3675 | if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) { |
| 3676 | Value *X; |
| 3677 | const APInt *Y; |
| 3678 | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
| 3679 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
| 3680 | Pred == ICmpInst::ICMP_EQ)) |
| 3681 | return V; |
| 3682 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3683 | // Comparing signed-less-than 0 checks if the sign bit is set. |
| 3684 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3685 | false)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3686 | return V; |
| 3687 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3688 | // Comparing signed-greater-than -1 checks if the sign bit is not set. |
| 3689 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3690 | true)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3691 | return V; |
| 3692 | } |
| 3693 | |
| 3694 | if (CondVal->hasOneUse()) { |
| 3695 | const APInt *C; |
| 3696 | if (match(CmpRHS, m_APInt(C))) { |
| 3697 | // X < MIN ? T : F --> F |
| 3698 | if (Pred == ICmpInst::ICMP_SLT && C->isMinSignedValue()) |
| 3699 | return FalseVal; |
| 3700 | // X < MIN ? T : F --> F |
| 3701 | if (Pred == ICmpInst::ICMP_ULT && C->isMinValue()) |
| 3702 | return FalseVal; |
| 3703 | // X > MAX ? T : F --> F |
| 3704 | if (Pred == ICmpInst::ICMP_SGT && C->isMaxSignedValue()) |
| 3705 | return FalseVal; |
| 3706 | // X > MAX ? T : F --> F |
| 3707 | if (Pred == ICmpInst::ICMP_UGT && C->isMaxValue()) |
| 3708 | return FalseVal; |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | // If we have an equality comparison, then we know the value in one of the |
| 3713 | // arms of the select. See if substituting this value into the arm and |
| 3714 | // simplifying the result yields the same value as the other arm. |
| 3715 | if (Pred == ICmpInst::ICMP_EQ) { |
| 3716 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3717 | TrueVal || |
| 3718 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3719 | TrueVal) |
| 3720 | return FalseVal; |
| 3721 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3722 | FalseVal || |
| 3723 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3724 | FalseVal) |
| 3725 | return FalseVal; |
| 3726 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 3727 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3728 | FalseVal || |
| 3729 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3730 | FalseVal) |
| 3731 | return TrueVal; |
| 3732 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3733 | TrueVal || |
| 3734 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3735 | TrueVal) |
| 3736 | return TrueVal; |
| 3737 | } |
| 3738 | |
| 3739 | return nullptr; |
| 3740 | } |
| 3741 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3742 | /// Given operands for a SelectInst, see if we can fold the result. |
| 3743 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3744 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 3745 | Value *FalseVal, const Query &Q, |
| 3746 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3747 | // select true, X, Y -> X |
| 3748 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3749 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 3750 | if (CB->isAllOnesValue()) |
| 3751 | return TrueVal; |
| 3752 | if (CB->isNullValue()) |
| 3753 | return FalseVal; |
| 3754 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3755 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3756 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3757 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3758 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3759 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3760 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3761 | if (isa<Constant>(TrueVal)) |
| 3762 | return TrueVal; |
| 3763 | return FalseVal; |
| 3764 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3765 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3766 | return FalseVal; |
| 3767 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3768 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3769 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3770 | if (Value *V = |
| 3771 | simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse)) |
| 3772 | return V; |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3773 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3774 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3777 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3778 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3779 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3780 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3781 | const Instruction *CxtI) { |
| 3782 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3783 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3786 | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
| 3787 | /// If not, this returns null. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3788 | static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3789 | const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3790 | // The type of the GEP pointer operand. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3791 | unsigned AS = |
| 3792 | cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3793 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3794 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3795 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3796 | return Ops[0]; |
| 3797 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3798 | // Compute the (pointer) type returned by the GEP instruction. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3799 | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1)); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3800 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3801 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3802 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
| 3803 | |
| 3804 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3805 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3806 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3807 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3808 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3809 | if (match(Ops[1], m_Zero())) |
| 3810 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3811 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3812 | Type *Ty = SrcTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3813 | if (Ty->isSized()) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3814 | Value *P; |
| 3815 | uint64_t C; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3816 | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3817 | // getelementptr P, N -> P if P points to a type of zero size. |
| 3818 | if (TyAllocSize == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3819 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3820 | |
| 3821 | // The following transforms are only safe if the ptrtoint cast |
| 3822 | // doesn't truncate the pointers. |
| 3823 | if (Ops[1]->getType()->getScalarSizeInBits() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3824 | Q.DL.getPointerSizeInBits(AS)) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3825 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3826 | if (match(P, m_Zero())) |
| 3827 | return Constant::getNullValue(GEPTy); |
| 3828 | Value *Temp; |
| 3829 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3830 | if (Temp->getType() == GEPTy) |
| 3831 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3832 | return nullptr; |
| 3833 | }; |
| 3834 | |
| 3835 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3836 | if (TyAllocSize == 1 && |
| 3837 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3838 | if (Value *R = PtrToIntOrZero(P)) |
| 3839 | return R; |
| 3840 | |
| 3841 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3842 | // if P points to a type of size 1 << C. |
| 3843 | if (match(Ops[1], |
| 3844 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3845 | m_ConstantInt(C))) && |
| 3846 | TyAllocSize == 1ULL << C) |
| 3847 | if (Value *R = PtrToIntOrZero(P)) |
| 3848 | return R; |
| 3849 | |
| 3850 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3851 | // if P points to a type of size C. |
| 3852 | if (match(Ops[1], |
| 3853 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3854 | m_SpecificInt(TyAllocSize)))) |
| 3855 | if (Value *R = PtrToIntOrZero(P)) |
| 3856 | return R; |
| 3857 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3858 | } |
| 3859 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3860 | |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3861 | if (Q.DL.getTypeAllocSize(LastType) == 1 && |
| 3862 | all_of(Ops.slice(1).drop_back(1), |
| 3863 | [](Value *Idx) { return match(Idx, m_Zero()); })) { |
| 3864 | unsigned PtrWidth = |
| 3865 | Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); |
| 3866 | if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) { |
| 3867 | APInt BasePtrOffset(PtrWidth, 0); |
| 3868 | Value *StrippedBasePtr = |
| 3869 | Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, |
| 3870 | BasePtrOffset); |
| 3871 | |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3872 | // gep (gep V, C), (sub 0, V) -> C |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3873 | if (match(Ops.back(), |
| 3874 | m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { |
| 3875 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
| 3876 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3877 | } |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3878 | // gep (gep V, C), (xor V, -1) -> C-1 |
| 3879 | if (match(Ops.back(), |
| 3880 | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) { |
| 3881 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
| 3882 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3883 | } |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3887 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3888 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3889 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3890 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3891 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3892 | return ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]), |
| 3893 | Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3896 | Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3897 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3898 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3899 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3900 | const Instruction *CxtI) { |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3901 | return ::SimplifyGEPInst(SrcTy, Ops, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3902 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3905 | /// Given operands for an InsertValueInst, see if we can fold the result. |
| 3906 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3907 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3908 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 3909 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3910 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3911 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3912 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3913 | |
| 3914 | // insertvalue x, undef, n -> x |
| 3915 | if (match(Val, m_Undef())) |
| 3916 | return Agg; |
| 3917 | |
| 3918 | // insertvalue x, (extractvalue y, n), n |
| 3919 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3920 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3921 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3922 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3923 | if (match(Agg, m_Undef())) |
| 3924 | return EV->getAggregateOperand(); |
| 3925 | |
| 3926 | // insertvalue y, (extractvalue y, n), n -> y |
| 3927 | if (Agg == EV->getAggregateOperand()) |
| 3928 | return Agg; |
| 3929 | } |
| 3930 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3931 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3934 | Value *llvm::SimplifyInsertValueInst( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3935 | Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3936 | const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3937 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3938 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3939 | RecursionLimit); |
| 3940 | } |
| 3941 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3942 | /// Given operands for an ExtractValueInst, see if we can fold the result. |
| 3943 | /// If not, this returns null. |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3944 | static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3945 | const Query &, unsigned) { |
| 3946 | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
| 3947 | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
| 3948 | |
| 3949 | // extractvalue x, (insertvalue y, elt, n), n -> elt |
| 3950 | unsigned NumIdxs = Idxs.size(); |
| 3951 | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
| 3952 | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) { |
| 3953 | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
| 3954 | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
| 3955 | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
| 3956 | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
| 3957 | Idxs.slice(0, NumCommonIdxs)) { |
| 3958 | if (NumIdxs == NumInsertValueIdxs) |
| 3959 | return IVI->getInsertedValueOperand(); |
| 3960 | break; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | return nullptr; |
| 3965 | } |
| 3966 | |
| 3967 | Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3968 | const DataLayout &DL, |
| 3969 | const TargetLibraryInfo *TLI, |
| 3970 | const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3971 | AssumptionCache *AC, |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3972 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3973 | return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3974 | RecursionLimit); |
| 3975 | } |
| 3976 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3977 | /// Given operands for an ExtractElementInst, see if we can fold the result. |
| 3978 | /// If not, this returns null. |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3979 | static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &, |
| 3980 | unsigned) { |
| 3981 | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
| 3982 | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
| 3983 | return ConstantFoldExtractElementInstruction(CVec, CIdx); |
| 3984 | |
| 3985 | // The index is not relevant if our vector is a splat. |
| 3986 | if (auto *Splat = CVec->getSplatValue()) |
| 3987 | return Splat; |
| 3988 | |
| 3989 | if (isa<UndefValue>(Vec)) |
| 3990 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 3991 | } |
| 3992 | |
| 3993 | // If extracting a specified index from the vector, see if we can recursively |
| 3994 | // find a previously computed scalar that was inserted into the vector. |
David Majnemer | 8e335ca | 2015-08-18 22:18:22 +0000 | [diff] [blame] | 3995 | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) |
| 3996 | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3997 | return Elt; |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3998 | |
| 3999 | return nullptr; |
| 4000 | } |
| 4001 | |
| 4002 | Value *llvm::SimplifyExtractElementInst( |
| 4003 | Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4004 | const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) { |
| 4005 | return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4006 | RecursionLimit); |
| 4007 | } |
| 4008 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4009 | /// See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4010 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4011 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 4012 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4013 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4014 | bool HasUndefInput = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 4015 | for (Value *Incoming : PN->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4016 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 4017 | if (Incoming == PN) continue; |
| 4018 | if (isa<UndefValue>(Incoming)) { |
| 4019 | // Remember that we saw an undef value, but otherwise ignore them. |
| 4020 | HasUndefInput = true; |
| 4021 | continue; |
| 4022 | } |
| 4023 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4024 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4025 | CommonValue = Incoming; |
| 4026 | } |
| 4027 | |
| 4028 | // If CommonValue is null then all of the incoming values were either undef or |
| 4029 | // equal to the phi node itself. |
| 4030 | if (!CommonValue) |
| 4031 | return UndefValue::get(PN->getType()); |
| 4032 | |
| 4033 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 4034 | // instruction, we cannot return X as the result of the PHI node unless it |
| 4035 | // dominates the PHI block. |
| 4036 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4037 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4038 | |
| 4039 | return CommonValue; |
| 4040 | } |
| 4041 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4042 | static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, |
| 4043 | Type *Ty, const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 126de5d | 2016-07-25 03:39:21 +0000 | [diff] [blame] | 4044 | if (auto *C = dyn_cast<Constant>(Op)) |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4045 | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 4046 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4047 | if (auto *CI = dyn_cast<CastInst>(Op)) { |
| 4048 | auto *Src = CI->getOperand(0); |
| 4049 | Type *SrcTy = Src->getType(); |
| 4050 | Type *MidTy = CI->getType(); |
| 4051 | Type *DstTy = Ty; |
| 4052 | if (Src->getType() == Ty) { |
| 4053 | auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode()); |
| 4054 | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
| 4055 | Type *SrcIntPtrTy = |
| 4056 | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr; |
| 4057 | Type *MidIntPtrTy = |
| 4058 | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr; |
| 4059 | Type *DstIntPtrTy = |
| 4060 | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr; |
| 4061 | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
| 4062 | SrcIntPtrTy, MidIntPtrTy, |
| 4063 | DstIntPtrTy) == Instruction::BitCast) |
| 4064 | return Src; |
| 4065 | } |
| 4066 | } |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4067 | |
| 4068 | // bitcast x -> x |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4069 | if (CastOpc == Instruction::BitCast) |
| 4070 | if (Op->getType() == Ty) |
| 4071 | return Op; |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4072 | |
| 4073 | return nullptr; |
| 4074 | } |
| 4075 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4076 | Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
| 4077 | const DataLayout &DL, |
| 4078 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4079 | const DominatorTree *DT, AssumptionCache *AC, |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4080 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4081 | return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4082 | RecursionLimit); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4085 | static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, |
| 4086 | Type *RetTy, const Query &Q, |
| 4087 | unsigned MaxRecurse) { |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame^] | 4088 | Type *InVecTy = Op0->getType(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4089 | unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame^] | 4090 | unsigned InVecNumElts = InVecTy->getVectorNumElements(); |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4091 | |
| 4092 | auto *Op0Const = dyn_cast<Constant>(Op0); |
| 4093 | auto *Op1Const = dyn_cast<Constant>(Op1); |
| 4094 | |
| 4095 | // If all operands are constant, constant fold the shuffle. |
| 4096 | if (Op0Const && Op1Const) |
| 4097 | return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask); |
| 4098 | |
| 4099 | // If only one of the operands is constant, constant fold the shuffle if the |
| 4100 | // mask does not select elements from the variable operand. |
| 4101 | bool MaskSelects0 = false, MaskSelects1 = false; |
| 4102 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
| 4103 | int Idx = ShuffleVectorInst::getMaskValue(Mask, i); |
| 4104 | if (Idx == -1) |
| 4105 | continue; |
| 4106 | if ((unsigned)Idx < InVecNumElts) |
| 4107 | MaskSelects0 = true; |
| 4108 | else |
| 4109 | MaskSelects1 = true; |
| 4110 | } |
| 4111 | if (!MaskSelects0 && Op1Const) |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame^] | 4112 | return ConstantFoldShuffleVectorInstruction(UndefValue::get(InVecTy), |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4113 | Op1Const, Mask); |
| 4114 | if (!MaskSelects1 && Op0Const) |
Zvi Rackover | 30efd24d | 2017-04-11 21:37:02 +0000 | [diff] [blame^] | 4115 | return ConstantFoldShuffleVectorInstruction(Op0Const, |
| 4116 | UndefValue::get(InVecTy), Mask); |
| 4117 | |
| 4118 | // A shuffle of a splat is always the splat itself. Legal if the shuffle's |
| 4119 | // value type is same as the input vectors' type. |
| 4120 | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0)) |
| 4121 | if (!MaskSelects1 && RetTy == InVecTy && |
| 4122 | OpShuf->getMask()->getSplatValue()) |
| 4123 | return Op0; |
| 4124 | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op1)) |
| 4125 | if (!MaskSelects0 && RetTy == InVecTy && |
| 4126 | OpShuf->getMask()->getSplatValue()) |
| 4127 | return Op1; |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4128 | |
| 4129 | return nullptr; |
| 4130 | } |
| 4131 | |
| 4132 | /// Given operands for a ShuffleVectorInst, fold the result or return null. |
| 4133 | Value *llvm::SimplifyShuffleVectorInst( |
| 4134 | Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, |
| 4135 | const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT, |
| 4136 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4137 | return ::SimplifyShuffleVectorInst( |
| 4138 | Op0, Op1, Mask, RetTy, Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
| 4139 | } |
| 4140 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4141 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4142 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4143 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4144 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4145 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4146 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4147 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4148 | case Instruction::Add: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4149 | return SimplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4150 | case Instruction::FAdd: |
| 4151 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4152 | case Instruction::Sub: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4153 | return SimplifySubInst(LHS, RHS, false, false, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4154 | case Instruction::FSub: |
| 4155 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4156 | case Instruction::Mul: |
| 4157 | return SimplifyMulInst(LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4158 | case Instruction::FMul: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4159 | return SimplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4160 | case Instruction::SDiv: |
| 4161 | return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 4162 | case Instruction::UDiv: |
| 4163 | return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4164 | case Instruction::FDiv: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4165 | return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4166 | case Instruction::SRem: |
| 4167 | return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 4168 | case Instruction::URem: |
| 4169 | return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4170 | case Instruction::FRem: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4171 | return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4172 | case Instruction::Shl: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4173 | return SimplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4174 | case Instruction::LShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4175 | return SimplifyLShrInst(LHS, RHS, false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4176 | case Instruction::AShr: |
Sanjay Patel | 1fd16f0 | 2017-04-01 18:40:30 +0000 | [diff] [blame] | 4177 | return SimplifyAShrInst(LHS, RHS, false, Q, MaxRecurse); |
| 4178 | case Instruction::And: |
| 4179 | return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 4180 | case Instruction::Or: |
| 4181 | return SimplifyOrInst(LHS, RHS, Q, MaxRecurse); |
| 4182 | case Instruction::Xor: |
| 4183 | return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4184 | default: |
Craig Topper | 8ef20ea | 2017-04-06 18:59:08 +0000 | [diff] [blame] | 4185 | llvm_unreachable("Unexpected opcode"); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4186 | } |
| 4187 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4188 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4189 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4190 | /// If not, this returns null. |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4191 | /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the |
| 4192 | /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp. |
| 4193 | static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
| 4194 | const FastMathFlags &FMF, const Query &Q, |
| 4195 | unsigned MaxRecurse) { |
| 4196 | switch (Opcode) { |
| 4197 | case Instruction::FAdd: |
| 4198 | return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4199 | case Instruction::FSub: |
| 4200 | return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4201 | case Instruction::FMul: |
| 4202 | return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 4203 | case Instruction::FDiv: |
| 4204 | return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4205 | default: |
| 4206 | return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
| 4207 | } |
| 4208 | } |
| 4209 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4210 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4211 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4212 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4213 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4214 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4215 | RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4218 | Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4219 | const FastMathFlags &FMF, const DataLayout &DL, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4220 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4221 | const DominatorTree *DT, AssumptionCache *AC, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4222 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4223 | return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI), |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4224 | RecursionLimit); |
| 4225 | } |
| 4226 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4227 | /// Given operands for a CmpInst, see if we can fold the result. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4228 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4229 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4230 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4231 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4232 | return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4236 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4237 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4238 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4239 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4240 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4241 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4242 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4243 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 4244 | switch (ID) { |
| 4245 | default: return false; |
| 4246 | |
| 4247 | // Unary idempotent: f(f(x)) = f(x) |
| 4248 | case Intrinsic::fabs: |
| 4249 | case Intrinsic::floor: |
| 4250 | case Intrinsic::ceil: |
| 4251 | case Intrinsic::trunc: |
| 4252 | case Intrinsic::rint: |
| 4253 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4254 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4255 | return true; |
| 4256 | } |
| 4257 | } |
| 4258 | |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4259 | static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
| 4260 | const DataLayout &DL) { |
| 4261 | GlobalValue *PtrSym; |
| 4262 | APInt PtrOffset; |
| 4263 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
| 4264 | return nullptr; |
| 4265 | |
| 4266 | Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext()); |
| 4267 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
| 4268 | Type *Int32PtrTy = Int32Ty->getPointerTo(); |
| 4269 | Type *Int64Ty = Type::getInt64Ty(Ptr->getContext()); |
| 4270 | |
| 4271 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
| 4272 | if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64) |
| 4273 | return nullptr; |
| 4274 | |
| 4275 | uint64_t OffsetInt = OffsetConstInt->getSExtValue(); |
| 4276 | if (OffsetInt % 4 != 0) |
| 4277 | return nullptr; |
| 4278 | |
| 4279 | Constant *C = ConstantExpr::getGetElementPtr( |
| 4280 | Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy), |
| 4281 | ConstantInt::get(Int64Ty, OffsetInt / 4)); |
| 4282 | Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL); |
| 4283 | if (!Loaded) |
| 4284 | return nullptr; |
| 4285 | |
| 4286 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
| 4287 | if (!LoadedCE) |
| 4288 | return nullptr; |
| 4289 | |
| 4290 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
| 4291 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4292 | if (!LoadedCE) |
| 4293 | return nullptr; |
| 4294 | } |
| 4295 | |
| 4296 | if (LoadedCE->getOpcode() != Instruction::Sub) |
| 4297 | return nullptr; |
| 4298 | |
| 4299 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4300 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
| 4301 | return nullptr; |
| 4302 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
| 4303 | |
| 4304 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
| 4305 | GlobalValue *LoadedRHSSym; |
| 4306 | APInt LoadedRHSOffset; |
| 4307 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
| 4308 | DL) || |
| 4309 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
| 4310 | return nullptr; |
| 4311 | |
| 4312 | return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy); |
| 4313 | } |
| 4314 | |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4315 | static bool maskIsAllZeroOrUndef(Value *Mask) { |
| 4316 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 4317 | if (!ConstMask) |
| 4318 | return false; |
| 4319 | if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask)) |
| 4320 | return true; |
| 4321 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 4322 | ++I) { |
| 4323 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 4324 | if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt)) |
| 4325 | continue; |
| 4326 | return false; |
| 4327 | } |
| 4328 | return true; |
| 4329 | } |
| 4330 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4331 | template <typename IterTy> |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4332 | static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4333 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4334 | Intrinsic::ID IID = F->getIntrinsicID(); |
| 4335 | unsigned NumOperands = std::distance(ArgBegin, ArgEnd); |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4336 | |
| 4337 | // Unary Ops |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4338 | if (NumOperands == 1) { |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4339 | // Perform idempotent optimizations |
| 4340 | if (IsIdempotent(IID)) { |
| 4341 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) { |
| 4342 | if (II->getIntrinsicID() == IID) |
| 4343 | return II; |
| 4344 | } |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | switch (IID) { |
| 4348 | case Intrinsic::fabs: { |
| 4349 | if (SignBitMustBeZero(*ArgBegin, Q.TLI)) |
| 4350 | return *ArgBegin; |
Marcello Maggioni | 0616b5f | 2017-01-14 07:28:47 +0000 | [diff] [blame] | 4351 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4352 | } |
| 4353 | default: |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4354 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4357 | |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4358 | // Binary Ops |
| 4359 | if (NumOperands == 2) { |
| 4360 | Value *LHS = *ArgBegin; |
| 4361 | Value *RHS = *(ArgBegin + 1); |
| 4362 | Type *ReturnType = F->getReturnType(); |
| 4363 | |
| 4364 | switch (IID) { |
| 4365 | case Intrinsic::usub_with_overflow: |
| 4366 | case Intrinsic::ssub_with_overflow: { |
| 4367 | // X - X -> { 0, false } |
| 4368 | if (LHS == RHS) |
| 4369 | return Constant::getNullValue(ReturnType); |
| 4370 | |
| 4371 | // X - undef -> undef |
| 4372 | // undef - X -> undef |
| 4373 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
| 4374 | return UndefValue::get(ReturnType); |
| 4375 | |
| 4376 | return nullptr; |
| 4377 | } |
| 4378 | case Intrinsic::uadd_with_overflow: |
| 4379 | case Intrinsic::sadd_with_overflow: { |
| 4380 | // X + undef -> undef |
| 4381 | if (isa<UndefValue>(RHS)) |
| 4382 | return UndefValue::get(ReturnType); |
| 4383 | |
| 4384 | return nullptr; |
| 4385 | } |
| 4386 | case Intrinsic::umul_with_overflow: |
| 4387 | case Intrinsic::smul_with_overflow: { |
| 4388 | // X * 0 -> { 0, false } |
| 4389 | if (match(RHS, m_Zero())) |
| 4390 | return Constant::getNullValue(ReturnType); |
| 4391 | |
| 4392 | // X * undef -> { 0, false } |
| 4393 | if (match(RHS, m_Undef())) |
| 4394 | return Constant::getNullValue(ReturnType); |
| 4395 | |
| 4396 | return nullptr; |
| 4397 | } |
| 4398 | case Intrinsic::load_relative: { |
| 4399 | Constant *C0 = dyn_cast<Constant>(LHS); |
| 4400 | Constant *C1 = dyn_cast<Constant>(RHS); |
| 4401 | if (C0 && C1) |
| 4402 | return SimplifyRelativeLoad(C0, C1, Q.DL); |
| 4403 | return nullptr; |
| 4404 | } |
| 4405 | default: |
| 4406 | return nullptr; |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | // Simplify calls to llvm.masked.load.* |
| 4411 | switch (IID) { |
| 4412 | case Intrinsic::masked_load: { |
| 4413 | Value *MaskArg = ArgBegin[2]; |
| 4414 | Value *PassthruArg = ArgBegin[3]; |
| 4415 | // If the mask is all zeros or undef, the "passthru" argument is the result. |
| 4416 | if (maskIsAllZeroOrUndef(MaskArg)) |
| 4417 | return PassthruArg; |
| 4418 | return nullptr; |
| 4419 | } |
| 4420 | default: |
| 4421 | return nullptr; |
| 4422 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4425 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4426 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4427 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4428 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4429 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 4430 | Ty = PTy->getElementType(); |
| 4431 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 4432 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4433 | // call undef -> undef |
David Majnemer | bb53d23 | 2016-06-25 07:37:30 +0000 | [diff] [blame] | 4434 | // call null -> undef |
| 4435 | if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4436 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4437 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4438 | Function *F = dyn_cast<Function>(V); |
| 4439 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4440 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4441 | |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4442 | if (F->isIntrinsic()) |
| 4443 | if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse)) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4444 | return Ret; |
| 4445 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4446 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4447 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4448 | |
| 4449 | SmallVector<Constant *, 4> ConstantArgs; |
| 4450 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 4451 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 4452 | Constant *C = dyn_cast<Constant>(*I); |
| 4453 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4454 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4455 | ConstantArgs.push_back(C); |
| 4456 | } |
| 4457 | |
| 4458 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4461 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4462 | User::op_iterator ArgEnd, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4463 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4464 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4465 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4466 | RecursionLimit); |
| 4467 | } |
| 4468 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4469 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4470 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4471 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4472 | const Instruction *CxtI) { |
| 4473 | return ::SimplifyCall(V, Args.begin(), Args.end(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4474 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4477 | /// See if we can compute a simplified version of this instruction. |
| 4478 | /// If not, this returns null. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4479 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 4480 | const TargetLibraryInfo *TLI, |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 4481 | const DominatorTree *DT, AssumptionCache *AC, |
| 4482 | OptimizationRemarkEmitter *ORE) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4483 | Value *Result; |
| 4484 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4485 | switch (I->getOpcode()) { |
| 4486 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 4487 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4488 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4489 | case Instruction::FAdd: |
| 4490 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4491 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4492 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 4493 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4494 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 4495 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4496 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4497 | TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4498 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4499 | case Instruction::FSub: |
| 4500 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4501 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4502 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4503 | case Instruction::Sub: |
| 4504 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 4505 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4506 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4507 | TLI, DT, AC, I); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4508 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4509 | case Instruction::FMul: |
| 4510 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4511 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4512 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4513 | case Instruction::Mul: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4514 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4515 | SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4516 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4517 | case Instruction::SDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4518 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4519 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4520 | break; |
| 4521 | case Instruction::UDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4522 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4523 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4524 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4525 | case Instruction::FDiv: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4526 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4527 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4528 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4529 | case Instruction::SRem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4530 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4531 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4532 | break; |
| 4533 | case Instruction::URem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4534 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4535 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4536 | break; |
| 4537 | case Instruction::FRem: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4538 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4539 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4540 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4541 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4542 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 4543 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4544 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4545 | TLI, DT, AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4546 | break; |
| 4547 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4548 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4549 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4550 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4551 | break; |
| 4552 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4553 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4554 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4555 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4556 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4557 | case Instruction::And: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4558 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4559 | SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4560 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4561 | case Instruction::Or: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4562 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4563 | SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4564 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4565 | case Instruction::Xor: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4566 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4567 | SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4568 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4569 | case Instruction::ICmp: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4570 | Result = |
| 4571 | SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), I->getOperand(0), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4572 | I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4573 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4574 | case Instruction::FCmp: |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4575 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 4576 | I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4577 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4578 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 4579 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4580 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4581 | I->getOperand(2), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4582 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4583 | case Instruction::GetElementPtr: { |
| 4584 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 4585 | Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4586 | Ops, DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4587 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4588 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4589 | case Instruction::InsertValue: { |
| 4590 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 4591 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 4592 | IV->getInsertedValueOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4593 | IV->getIndices(), DL, TLI, DT, AC, I); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4594 | break; |
| 4595 | } |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4596 | case Instruction::ExtractValue: { |
| 4597 | auto *EVI = cast<ExtractValueInst>(I); |
| 4598 | Result = SimplifyExtractValueInst(EVI->getAggregateOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4599 | EVI->getIndices(), DL, TLI, DT, AC, I); |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4600 | break; |
| 4601 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4602 | case Instruction::ExtractElement: { |
| 4603 | auto *EEI = cast<ExtractElementInst>(I); |
| 4604 | Result = SimplifyExtractElementInst( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4605 | EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4606 | break; |
| 4607 | } |
Zvi Rackover | 8f46065 | 2017-04-03 22:05:30 +0000 | [diff] [blame] | 4608 | case Instruction::ShuffleVector: { |
| 4609 | auto *SVI = cast<ShuffleVectorInst>(I); |
| 4610 | Result = SimplifyShuffleVectorInst(SVI->getOperand(0), SVI->getOperand(1), |
| 4611 | SVI->getMask(), SVI->getType(), DL, TLI, |
| 4612 | DT, AC, I); |
| 4613 | break; |
| 4614 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 4615 | case Instruction::PHI: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4616 | Result = SimplifyPHINode(cast<PHINode>(I), Query(DL, TLI, DT, AC, I)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4617 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4618 | case Instruction::Call: { |
| 4619 | CallSite CS(cast<CallInst>(I)); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4620 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4621 | TLI, DT, AC, I); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4622 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4623 | } |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4624 | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
| 4625 | #include "llvm/IR/Instruction.def" |
| 4626 | #undef HANDLE_CAST_INST |
| 4627 | Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4628 | DL, TLI, DT, AC, I); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4629 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4630 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4631 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4632 | // In general, it is possible for computeKnownBits to determine all bits in a |
| 4633 | // value even when the operands are not all constants. |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4634 | if (!Result && I->getType()->isIntOrIntVectorTy()) { |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4635 | unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
| 4636 | APInt KnownZero(BitWidth, 0); |
| 4637 | APInt KnownOne(BitWidth, 0); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 4638 | computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT, ORE); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4639 | if ((KnownZero | KnownOne).isAllOnesValue()) |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4640 | Result = ConstantInt::get(I->getType(), KnownOne); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4643 | /// If called on unreachable code, the above logic may report that the |
| 4644 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 4645 | /// detecting that case here, returning a safe value instead. |
| 4646 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
Sanjay Patel | f44bd38 | 2016-01-20 18:59:48 +0000 | [diff] [blame] | 4649 | /// \brief Implementation of recursive simplification through an instruction's |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4650 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4651 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4652 | /// This is the common implementation of the recursive simplification routines. |
| 4653 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 4654 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 4655 | /// instructions to process and attempt to simplify it using |
| 4656 | /// InstructionSimplify. |
| 4657 | /// |
| 4658 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 4659 | /// in simplified value does not count toward this. |
| 4660 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4661 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4662 | const DominatorTree *DT, |
| 4663 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4664 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4665 | SmallSetVector<Instruction *, 8> Worklist; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4666 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4667 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4668 | // If we have an explicit value to collapse to, do that round of the |
| 4669 | // simplification loop by hand initially. |
| 4670 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4671 | for (User *U : I->users()) |
| 4672 | if (U != I) |
| 4673 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4674 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4675 | // Replace the instruction with its simplified value. |
| 4676 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 4677 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4678 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4679 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4680 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4681 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4682 | I->eraseFromParent(); |
| 4683 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4684 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4685 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4686 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4687 | // Note that we must test the size on each iteration, the worklist can grow. |
| 4688 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 4689 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4690 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4691 | // See if this instruction simplifies. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4692 | SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4693 | if (!SimpleV) |
| 4694 | continue; |
| 4695 | |
| 4696 | Simplified = true; |
| 4697 | |
| 4698 | // Stash away all the uses of the old instruction so we can check them for |
| 4699 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 4700 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4701 | for (User *U : I->users()) |
| 4702 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Replace the instruction with its simplified value. |
| 4705 | I->replaceAllUsesWith(SimpleV); |
| 4706 | |
| 4707 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4708 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4709 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4710 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4711 | I->eraseFromParent(); |
| 4712 | } |
| 4713 | return Simplified; |
| 4714 | } |
| 4715 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4716 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4717 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4718 | const DominatorTree *DT, |
| 4719 | AssumptionCache *AC) { |
| 4720 | return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4724 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4725 | const DominatorTree *DT, |
| 4726 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4727 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 4728 | assert(SimpleV && "Must provide a simplified value."); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4729 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4730 | } |