Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1 | //===- InstructionSimplify.cpp - Fold instruction operands ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements routines for folding instructions into simpler forms |
Duncan Sands | a021988 | 2010-11-23 10:50:08 +0000 | [diff] [blame] | 11 | // that do not require creating new instructions. This does constant folding |
| 12 | // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either |
| 13 | // returning a constant ("and i32 %x, 0" -> "0") or an already existing value |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 14 | // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been |
| 15 | // simplified: This is usually true and assuming it simplifies the logic (if |
| 16 | // they have not been simplified then results are correct but maybe suboptimal). |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CaptureTracking.h" |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 29 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/GlobalAlias.h" |
| 34 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 35 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 36 | #include "llvm/IR/ValueHandle.h" |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 39 | using namespace llvm::PatternMatch; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 40 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "instsimplify" |
| 42 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 43 | enum { RecursionLimit = 3 }; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 44 | |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 45 | STATISTIC(NumExpand, "Number of expansions"); |
Duncan Sands | 3547d2e | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 46 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 47 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 48 | namespace { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 49 | struct Query { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 50 | const DataLayout &DL; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 51 | const TargetLibraryInfo *TLI; |
| 52 | const DominatorTree *DT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 53 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 54 | const Instruction *CxtI; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 55 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 56 | Query(const DataLayout &DL, const TargetLibraryInfo *tli, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 57 | const DominatorTree *dt, AssumptionCache *ac = nullptr, |
| 58 | const Instruction *cxti = nullptr) |
| 59 | : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {} |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 60 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 61 | } // end anonymous namespace |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 62 | |
| 63 | static Value *SimplifyAndInst(Value *, Value *, const Query &, unsigned); |
| 64 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 65 | unsigned); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 66 | static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
| 67 | const Query &, unsigned); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 68 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const Query &, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 69 | unsigned); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 70 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 71 | const Query &Q, unsigned MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 72 | static Value *SimplifyOrInst(Value *, Value *, const Query &, unsigned); |
| 73 | static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned); |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 74 | static Value *SimplifyCastInst(unsigned, Value *, Type *, |
| 75 | const Query &, unsigned); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 76 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 77 | /// 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] | 78 | /// a vector with every element false, as appropriate for the type. |
| 79 | static Constant *getFalse(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 80 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 81 | "Expected i1 type or a vector of i1!"); |
| 82 | return Constant::getNullValue(Ty); |
| 83 | } |
| 84 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 85 | /// 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] | 86 | /// a vector with every element true, as appropriate for the type. |
| 87 | static Constant *getTrue(Type *Ty) { |
Nick Lewycky | e659b84 | 2011-12-01 02:39:36 +0000 | [diff] [blame] | 88 | assert(Ty->getScalarType()->isIntegerTy(1) && |
Duncan Sands | c1c9271 | 2011-07-26 15:03:53 +0000 | [diff] [blame] | 89 | "Expected i1 type or a vector of i1!"); |
| 90 | return Constant::getAllOnesValue(Ty); |
| 91 | } |
| 92 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 93 | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
| 94 | static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, |
| 95 | Value *RHS) { |
| 96 | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
| 97 | if (!Cmp) |
| 98 | return false; |
| 99 | CmpInst::Predicate CPred = Cmp->getPredicate(); |
| 100 | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
| 101 | if (CPred == Pred && CLHS == LHS && CRHS == RHS) |
| 102 | return true; |
| 103 | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && |
| 104 | CRHS == LHS; |
| 105 | } |
| 106 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 107 | /// Does the given value dominate the specified phi node? |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 108 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 109 | Instruction *I = dyn_cast<Instruction>(V); |
| 110 | if (!I) |
| 111 | // Arguments and constants dominate all instructions. |
| 112 | return true; |
| 113 | |
Chandler Carruth | 3ffccb3 | 2012-03-21 10:58:47 +0000 | [diff] [blame] | 114 | // If we are processing instructions (and/or basic blocks) that have not been |
| 115 | // fully added to a function, the parent nodes may still be null. Simply |
| 116 | // return the conservative answer in these cases. |
| 117 | if (!I->getParent() || !P->getParent() || !I->getParent()->getParent()) |
| 118 | return false; |
| 119 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 120 | // If we have a DominatorTree then do a precise test. |
Eli Friedman | c8cbd06 | 2012-03-13 01:06:07 +0000 | [diff] [blame] | 121 | if (DT) { |
| 122 | if (!DT->isReachableFromEntry(P->getParent())) |
| 123 | return true; |
| 124 | if (!DT->isReachableFromEntry(I->getParent())) |
| 125 | return false; |
| 126 | return DT->dominates(I, P); |
| 127 | } |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 128 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 129 | // Otherwise, if the instruction is in the entry block and is not an invoke, |
| 130 | // then it obviously dominates all phi nodes. |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 131 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 132 | !isa<InvokeInst>(I)) |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 133 | return true; |
| 134 | |
| 135 | return false; |
| 136 | } |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 137 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 138 | /// Simplify "A op (B op' C)" by distributing op over op', turning it into |
| 139 | /// "(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] | 140 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 141 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 142 | /// Returns the simplified value, or null if no simplification was performed. |
| 143 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 144 | unsigned OpcToExpand, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 145 | unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 146 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
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. |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 202 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 203 | const Query &Q, unsigned MaxRecurse) { |
Benjamin Kramer | b6d52b8 | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 204 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
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. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 301 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 302 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 303 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 304 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 305 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 306 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 307 | SelectInst *SI; |
| 308 | if (isa<SelectInst>(LHS)) { |
| 309 | SI = cast<SelectInst>(LHS); |
| 310 | } else { |
| 311 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 312 | SI = cast<SelectInst>(RHS); |
| 313 | } |
| 314 | |
| 315 | // Evaluate the BinOp on the true and false branches of the select. |
| 316 | Value *TV; |
| 317 | Value *FV; |
| 318 | if (SI == LHS) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 319 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
| 320 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 321 | } else { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 322 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
| 323 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Duncan Sands | e3c5395 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 326 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 327 | // If they both failed to simplify then return null. |
| 328 | if (TV == FV) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 329 | return TV; |
| 330 | |
| 331 | // If one branch simplified to undef, return the other one. |
| 332 | if (TV && isa<UndefValue>(TV)) |
| 333 | return FV; |
| 334 | if (FV && isa<UndefValue>(FV)) |
| 335 | return TV; |
| 336 | |
| 337 | // If applying the operation did not change the true and false select values, |
| 338 | // then the result of the binop is the select itself. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 339 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 340 | return SI; |
| 341 | |
| 342 | // If one branch simplified and the other did not, and the simplified |
| 343 | // value is equal to the unsimplified one, return the simplified value. |
| 344 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 345 | if ((FV && !TV) || (TV && !FV)) { |
| 346 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 347 | // same as the original operation. |
| 348 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 349 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 350 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 351 | // We already know that "op" is the same as for the simplified value. See |
| 352 | // if the operands match too. If so, return the simplified value. |
| 353 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 354 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 355 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 356 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 357 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 358 | return Simplified; |
| 359 | if (Simplified->isCommutative() && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 360 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 361 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 362 | return Simplified; |
| 363 | } |
| 364 | } |
| 365 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 366 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 369 | /// In the case of a comparison with a select instruction, try to simplify the |
| 370 | /// comparison by seeing whether both branches of the select result in the same |
| 371 | /// value. Returns the common value if so, otherwise returns null. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 372 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 373 | Value *RHS, const Query &Q, |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 374 | unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 375 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 376 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 377 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 378 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 379 | // Make sure the select is on the LHS. |
| 380 | if (!isa<SelectInst>(LHS)) { |
| 381 | std::swap(LHS, RHS); |
| 382 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 383 | } |
| 384 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 385 | SelectInst *SI = cast<SelectInst>(LHS); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 386 | Value *Cond = SI->getCondition(); |
| 387 | Value *TV = SI->getTrueValue(); |
| 388 | Value *FV = SI->getFalseValue(); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 389 | |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 390 | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 391 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 392 | Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 393 | if (TCmp == Cond) { |
| 394 | // It not only simplified, it simplified to the select condition. Replace |
| 395 | // it with 'true'. |
| 396 | TCmp = getTrue(Cond->getType()); |
| 397 | } else if (!TCmp) { |
| 398 | // It didn't simplify. However if "cmp TV, RHS" is equal to the select |
| 399 | // condition then we can replace it with 'true'. Otherwise give up. |
| 400 | if (!isSameCompare(Cond, Pred, TV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 401 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 402 | TCmp = getTrue(Cond->getType()); |
Duncan Sands | 0650402 | 2011-02-03 09:37:39 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 405 | // Does "cmp FV, RHS" simplify? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 406 | Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse); |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 407 | if (FCmp == Cond) { |
| 408 | // It not only simplified, it simplified to the select condition. Replace |
| 409 | // it with 'false'. |
| 410 | FCmp = getFalse(Cond->getType()); |
| 411 | } else if (!FCmp) { |
| 412 | // It didn't simplify. However if "cmp FV, RHS" is equal to the select |
| 413 | // condition then we can replace it with 'false'. Otherwise give up. |
| 414 | if (!isSameCompare(Cond, Pred, FV, RHS)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 415 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 416 | FCmp = getFalse(Cond->getType()); |
| 417 | } |
| 418 | |
| 419 | // If both sides simplified to the same value, then use it as the result of |
| 420 | // the original comparison. |
| 421 | if (TCmp == FCmp) |
| 422 | return TCmp; |
Duncan Sands | 26641d7 | 2012-02-10 14:31:24 +0000 | [diff] [blame] | 423 | |
| 424 | // The remaining cases only make sense if the select condition has the same |
| 425 | // type as the result of the comparison, so bail out if this is not so. |
| 426 | if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 427 | return nullptr; |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 428 | // If the false value simplified to false, then the result of the compare |
| 429 | // is equal to "Cond && TCmp". This also catches the case when the false |
| 430 | // value simplified to false and the true value to true, returning "Cond". |
| 431 | if (match(FCmp, m_Zero())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 432 | if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 433 | return V; |
| 434 | // If the true value simplified to true, then the result of the compare |
| 435 | // is equal to "Cond || FCmp". |
| 436 | if (match(TCmp, m_One())) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 437 | if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 438 | return V; |
| 439 | // Finally, if the false value simplified to true and the true value to |
| 440 | // false, then the result of the compare is equal to "!Cond". |
| 441 | if (match(FCmp, m_One()) && match(TCmp, m_Zero())) |
| 442 | if (Value *V = |
| 443 | SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 444 | Q, MaxRecurse)) |
Duncan Sands | 3d5692a | 2011-10-30 19:56:36 +0000 | [diff] [blame] | 445 | return V; |
| 446 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 447 | return nullptr; |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 450 | /// In the case of a binary operation with an operand that is a PHI instruction, |
| 451 | /// try to simplify the binop by seeing whether evaluating it on the incoming |
| 452 | /// phi values yields the same result for every value. If so returns the common |
| 453 | /// value, otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 454 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 455 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 456 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 457 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 458 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 459 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 460 | PHINode *PI; |
| 461 | if (isa<PHINode>(LHS)) { |
| 462 | PI = cast<PHINode>(LHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 463 | // 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] | 464 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 465 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 466 | } else { |
| 467 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 468 | PI = cast<PHINode>(RHS); |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 469 | // 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] | 470 | if (!ValueDominatesPHI(LHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 471 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 475 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 476 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 477 | // 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] | 478 | if (Incoming == PI) continue; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 479 | Value *V = PI == LHS ? |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 480 | SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : |
| 481 | SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 482 | // If the operation failed to simplify, or simplified to a different value |
| 483 | // to previously, then give up. |
| 484 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 485 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 486 | CommonValue = V; |
| 487 | } |
| 488 | |
| 489 | return CommonValue; |
| 490 | } |
| 491 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 492 | /// In the case of a comparison with a PHI instruction, try to simplify the |
| 493 | /// comparison by seeing whether comparing with all of the incoming phi values |
| 494 | /// yields the same result every time. If so returns the common result, |
| 495 | /// otherwise returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 496 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 497 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 498 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 499 | if (!MaxRecurse--) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 500 | return nullptr; |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 501 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 502 | // Make sure the phi is on the LHS. |
| 503 | if (!isa<PHINode>(LHS)) { |
| 504 | std::swap(LHS, RHS); |
| 505 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 506 | } |
| 507 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 508 | PHINode *PI = cast<PHINode>(LHS); |
| 509 | |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 510 | // 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] | 511 | if (!ValueDominatesPHI(RHS, PI, Q.DT)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 512 | return nullptr; |
Duncan Sands | 5ffc298 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 513 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 514 | // Evaluate the BinOp on the incoming phi values. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 515 | Value *CommonValue = nullptr; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 516 | for (Value *Incoming : PI->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 517 | // 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] | 518 | if (Incoming == PI) continue; |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 519 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 520 | // If the operation failed to simplify, or simplified to a different value |
| 521 | // to previously, then give up. |
| 522 | if (!V || (CommonValue && V != CommonValue)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 523 | return nullptr; |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 524 | CommonValue = V; |
| 525 | } |
| 526 | |
| 527 | return CommonValue; |
| 528 | } |
| 529 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 530 | /// Given operands for an Add, see if we can fold the result. |
| 531 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 532 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 533 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 534 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 535 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 536 | return ConstantFoldBinaryOpOperands(Instruction::Add, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 538 | // Canonicalize the constant to the RHS. |
| 539 | std::swap(Op0, Op1); |
| 540 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 541 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 542 | // X + undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 543 | if (match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 544 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 545 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 546 | // X + 0 -> X |
| 547 | if (match(Op1, m_Zero())) |
| 548 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 549 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 550 | // X + (Y - X) -> Y |
| 551 | // (Y - X) + X -> Y |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 552 | // Eg: X + -X -> 0 |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 553 | Value *Y = nullptr; |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 554 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 555 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 556 | return Y; |
| 557 | |
| 558 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 559 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 560 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 561 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 562 | |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 563 | /// i1 add -> xor. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 564 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 565 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 566 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 567 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 568 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 569 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 570 | MaxRecurse)) |
| 571 | return V; |
| 572 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 573 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 574 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 575 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 576 | // only if B and C are equal. If B and C are equal then (since we assume |
| 577 | // that operands have already been simplified) "select(cond, B, C)" should |
| 578 | // have been simplified to the common value of B and C already. Analysing |
| 579 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 580 | // for threading over phi nodes. |
| 581 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 582 | return nullptr; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 585 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 586 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 587 | const DominatorTree *DT, AssumptionCache *AC, |
| 588 | const Instruction *CxtI) { |
| 589 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 590 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 593 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 594 | /// |
| 595 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 596 | /// accumulates the total constant offset applied in the returned constant. It |
| 597 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 598 | /// no constant offsets applied. |
Dan Gohman | 36fa839 | 2013-01-31 02:45:26 +0000 | [diff] [blame] | 599 | /// |
| 600 | /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't |
| 601 | /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. |
| 602 | /// folding. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 603 | static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 604 | bool AllowNonInbounds = false) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 605 | assert(V->getType()->getScalarType()->isPointerTy()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 606 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 607 | Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType(); |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 608 | APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth()); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 609 | |
| 610 | // Even though we don't look through PHI nodes, we could be called on an |
| 611 | // instruction in an unreachable block, which may be on a cycle. |
| 612 | SmallPtrSet<Value *, 4> Visited; |
| 613 | Visited.insert(V); |
| 614 | do { |
| 615 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 616 | if ((!AllowNonInbounds && !GEP->isInBounds()) || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 617 | !GEP->accumulateConstantOffset(DL, Offset)) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 618 | break; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 619 | V = GEP->getPointerOperand(); |
| 620 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Matt Arsenault | 2f9cce2 | 2013-08-03 01:03:12 +0000 | [diff] [blame] | 621 | V = cast<Operator>(V)->getOperand(0); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 622 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 623 | if (GA->isInterposable()) |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 624 | break; |
| 625 | V = GA->getAliasee(); |
| 626 | } else { |
Hal Finkel | 2cac58f | 2016-07-11 03:37:59 +0000 | [diff] [blame] | 627 | if (auto CS = CallSite(V)) |
| 628 | if (Value *RV = CS.getReturnedArgOperand()) { |
| 629 | V = RV; |
| 630 | continue; |
| 631 | } |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 632 | break; |
| 633 | } |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 634 | assert(V->getType()->getScalarType()->isPointerTy() && |
| 635 | "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 636 | } while (Visited.insert(V).second); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 637 | |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 638 | Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset); |
| 639 | if (V->getType()->isVectorTy()) |
| 640 | return ConstantVector::getSplat(V->getType()->getVectorNumElements(), |
| 641 | OffsetIntPtr); |
| 642 | return OffsetIntPtr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /// \brief Compute the constant difference between two pointer values. |
| 646 | /// If the difference is not a constant, returns zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 647 | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
| 648 | Value *RHS) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 649 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 650 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 651 | |
| 652 | // If LHS and RHS are not related via constant offsets to the same base |
| 653 | // value, there is nothing we can do here. |
| 654 | if (LHS != RHS) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 655 | return nullptr; |
Chandler Carruth | a079655 | 2012-03-12 11:19:31 +0000 | [diff] [blame] | 656 | |
| 657 | // Otherwise, the difference of LHS - RHS can be computed as: |
| 658 | // LHS - RHS |
| 659 | // = (LHSOffset + Base) - (RHSOffset + Base) |
| 660 | // = LHSOffset - RHSOffset |
| 661 | return ConstantExpr::getSub(LHSOffset, RHSOffset); |
| 662 | } |
| 663 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 664 | /// Given operands for a Sub, see if we can fold the result. |
| 665 | /// If not, this returns null. |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 666 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 667 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 668 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 669 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 670 | return ConstantFoldBinaryOpOperands(Instruction::Sub, CLHS, CRHS, Q.DL); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 671 | |
| 672 | // X - undef -> undef |
| 673 | // undef - X -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 674 | if (match(Op0, m_Undef()) || match(Op1, m_Undef())) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 675 | return UndefValue::get(Op0->getType()); |
| 676 | |
| 677 | // X - 0 -> X |
| 678 | if (match(Op1, m_Zero())) |
| 679 | return Op0; |
| 680 | |
| 681 | // X - X -> 0 |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 682 | if (Op0 == Op1) |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 683 | return Constant::getNullValue(Op0->getType()); |
| 684 | |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 685 | // Is this a negation? |
| 686 | if (match(Op0, m_Zero())) { |
| 687 | // 0 - X -> 0 if the sub is NUW. |
| 688 | if (isNUW) |
| 689 | return Op0; |
| 690 | |
| 691 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 692 | APInt KnownZero(BitWidth, 0); |
| 693 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 694 | computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); |
Sanjay Patel | efd8885 | 2016-10-19 21:23:45 +0000 | [diff] [blame] | 695 | if (KnownZero == ~APInt::getSignBit(BitWidth)) { |
| 696 | // Op1 is either 0 or the minimum signed value. If the sub is NSW, then |
| 697 | // Op1 must be 0 because negating the minimum signed value is undefined. |
| 698 | if (isNSW) |
| 699 | return Op0; |
| 700 | |
| 701 | // 0 - X -> X if X is 0 or the minimum signed value. |
| 702 | return Op1; |
| 703 | } |
| 704 | } |
David Majnemer | cd4fbcd | 2014-07-31 04:49:18 +0000 | [diff] [blame] | 705 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 706 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 707 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
Dinesh Dwivedi | 99281a0 | 2014-06-26 08:57:33 +0000 | [diff] [blame] | 708 | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 709 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 710 | // See if "V === Y - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 711 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 712 | // It does! Now see if "X + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 713 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 714 | // It does, we successfully reassociated! |
| 715 | ++NumReassoc; |
| 716 | return W; |
| 717 | } |
| 718 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 719 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 720 | // It does! Now see if "Y + V" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 721 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 722 | // It does, we successfully reassociated! |
| 723 | ++NumReassoc; |
| 724 | return W; |
| 725 | } |
| 726 | } |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 727 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 728 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 729 | // For example, X - (X + 1) -> -1 |
| 730 | X = Op0; |
| 731 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 732 | // See if "V === X - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 733 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 734 | // It does! Now see if "V - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 735 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 736 | // It does, we successfully reassociated! |
| 737 | ++NumReassoc; |
| 738 | return W; |
| 739 | } |
| 740 | // See if "V === X - Z" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 741 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 742 | // It does! Now see if "V - Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 743 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 744 | // It does, we successfully reassociated! |
| 745 | ++NumReassoc; |
| 746 | return W; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 751 | // For example, X - (X - Y) -> Y. |
| 752 | Z = Op0; |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 753 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 754 | // See if "V === Z - X" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 755 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 756 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 757 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { |
Duncan Sands | d6f1a95 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 758 | // It does, we successfully reassociated! |
| 759 | ++NumReassoc; |
| 760 | return W; |
| 761 | } |
| 762 | |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 763 | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
| 764 | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) && |
| 765 | match(Op1, m_Trunc(m_Value(Y)))) |
| 766 | if (X->getType() == Y->getType()) |
| 767 | // See if "V === X - Y" simplifies. |
| 768 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) |
| 769 | // It does! Now see if "trunc V" simplifies. |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 770 | if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
| 771 | Q, MaxRecurse - 1)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 772 | // It does, return the simplified "trunc V". |
| 773 | return W; |
| 774 | |
| 775 | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 776 | if (match(Op0, m_PtrToInt(m_Value(X))) && |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 777 | match(Op1, m_PtrToInt(m_Value(Y)))) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 778 | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 779 | return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); |
| 780 | |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 781 | // i1 sub -> xor. |
| 782 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 783 | if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | 99589d0 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 784 | return V; |
| 785 | |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 786 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 787 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 788 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 789 | // only if B and C are equal. If B and C are equal then (since we assume |
| 790 | // that operands have already been simplified) "select(cond, B, C)" should |
| 791 | // have been simplified to the common value of B and C already. Analysing |
| 792 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 793 | // for threading over phi nodes. |
| 794 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 795 | return nullptr; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 798 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 799 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 800 | const DominatorTree *DT, AssumptionCache *AC, |
| 801 | const Instruction *CxtI) { |
| 802 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 803 | RecursionLimit); |
Duncan Sands | ed6d6c3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 806 | /// Given operands for an FAdd, see if we can fold the result. If not, this |
| 807 | /// returns null. |
| 808 | static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 809 | const Query &Q, unsigned MaxRecurse) { |
| 810 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 811 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 812 | return ConstantFoldBinaryOpOperands(Instruction::FAdd, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 813 | |
| 814 | // Canonicalize the constant to the RHS. |
| 815 | std::swap(Op0, Op1); |
| 816 | } |
| 817 | |
| 818 | // fadd X, -0 ==> X |
| 819 | if (match(Op1, m_NegZero())) |
| 820 | return Op0; |
| 821 | |
| 822 | // fadd X, 0 ==> X, when we know X is not -0 |
| 823 | if (match(Op1, m_Zero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 824 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 825 | return Op0; |
| 826 | |
| 827 | // fadd [nnan ninf] X, (fsub [nnan ninf] 0, X) ==> 0 |
| 828 | // where nnan and ninf have to occur at least once somewhere in this |
| 829 | // expression |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 830 | Value *SubOp = nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 831 | if (match(Op1, m_FSub(m_AnyZero(), m_Specific(Op0)))) |
| 832 | SubOp = Op1; |
| 833 | else if (match(Op0, m_FSub(m_AnyZero(), m_Specific(Op1)))) |
| 834 | SubOp = Op0; |
| 835 | if (SubOp) { |
| 836 | Instruction *FSub = cast<Instruction>(SubOp); |
| 837 | if ((FMF.noNaNs() || FSub->hasNoNaNs()) && |
| 838 | (FMF.noInfs() || FSub->hasNoInfs())) |
| 839 | return Constant::getNullValue(Op0->getType()); |
| 840 | } |
| 841 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 842 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /// Given operands for an FSub, see if we can fold the result. If not, this |
| 846 | /// returns null. |
| 847 | static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 848 | const Query &Q, unsigned MaxRecurse) { |
| 849 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 850 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 851 | return ConstantFoldBinaryOpOperands(Instruction::FSub, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | // fsub X, 0 ==> X |
| 855 | if (match(Op1, m_Zero())) |
| 856 | return Op0; |
| 857 | |
| 858 | // fsub X, -0 ==> X, when we know X is not -0 |
| 859 | if (match(Op1, m_NegZero()) && |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 860 | (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI))) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 861 | return Op0; |
| 862 | |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 863 | // fsub -0.0, (fsub -0.0, X) ==> X |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 864 | Value *X; |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 865 | if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X)))) |
| 866 | return X; |
| 867 | |
| 868 | // 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] | 869 | if (FMF.noSignedZeros() && match(Op0, m_AnyZero()) && |
Benjamin Kramer | f5b2a47 | 2016-02-29 11:12:23 +0000 | [diff] [blame] | 870 | match(Op1, m_FSub(m_AnyZero(), m_Value(X)))) |
| 871 | return X; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 872 | |
Benjamin Kramer | 228680d | 2015-06-14 21:01:20 +0000 | [diff] [blame] | 873 | // fsub nnan x, x ==> 0.0 |
| 874 | if (FMF.noNaNs() && Op0 == Op1) |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 875 | return Constant::getNullValue(Op0->getType()); |
| 876 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 877 | return nullptr; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 880 | /// Given the operands for an FMul, see if we can fold the result |
| 881 | static Value *SimplifyFMulInst(Value *Op0, Value *Op1, |
| 882 | FastMathFlags FMF, |
| 883 | const Query &Q, |
| 884 | unsigned MaxRecurse) { |
| 885 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 886 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 887 | return ConstantFoldBinaryOpOperands(Instruction::FMul, CLHS, CRHS, Q.DL); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 888 | |
| 889 | // Canonicalize the constant to the RHS. |
| 890 | std::swap(Op0, Op1); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 893 | // fmul X, 1.0 ==> X |
| 894 | if (match(Op1, m_FPOne())) |
| 895 | return Op0; |
| 896 | |
| 897 | // fmul nnan nsz X, 0 ==> 0 |
| 898 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero())) |
| 899 | return Op1; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 900 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 901 | return nullptr; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 904 | /// Given operands for a Mul, see if we can fold the result. |
| 905 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 906 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, |
| 907 | unsigned MaxRecurse) { |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 908 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 909 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 910 | return ConstantFoldBinaryOpOperands(Instruction::Mul, CLHS, CRHS, Q.DL); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 911 | |
| 912 | // Canonicalize the constant to the RHS. |
| 913 | std::swap(Op0, Op1); |
| 914 | } |
| 915 | |
| 916 | // X * undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 917 | if (match(Op1, m_Undef())) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 918 | return Constant::getNullValue(Op0->getType()); |
| 919 | |
| 920 | // X * 0 -> 0 |
| 921 | if (match(Op1, m_Zero())) |
| 922 | return Op1; |
| 923 | |
| 924 | // X * 1 -> X |
| 925 | if (match(Op1, m_One())) |
| 926 | return Op0; |
| 927 | |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 928 | // (X / Y) * Y -> X if the division is exact. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 929 | Value *X = nullptr; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 930 | if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
| 931 | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y) |
| 932 | return X; |
Duncan Sands | b67edc6 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 933 | |
Nick Lewycky | b89d9a4 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 934 | // i1 mul -> and. |
Duncan Sands | 5def0d6 | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 935 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 936 | if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) |
Duncan Sands | fecc642 | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 937 | return V; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 938 | |
| 939 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 940 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 941 | MaxRecurse)) |
| 942 | return V; |
| 943 | |
| 944 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 945 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 946 | Q, MaxRecurse)) |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 947 | return V; |
| 948 | |
| 949 | // If the operation is with the result of a select instruction, check whether |
| 950 | // operating on either branch of the select always yields the same value. |
| 951 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 952 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 953 | MaxRecurse)) |
| 954 | return V; |
| 955 | |
| 956 | // If the operation is with the result of a phi instruction, check whether |
| 957 | // operating on all incoming values of the phi always yields the same value. |
| 958 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 959 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 960 | MaxRecurse)) |
| 961 | return V; |
| 962 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 963 | return nullptr; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 966 | Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 967 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 968 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 969 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 970 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 971 | return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 972 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 976 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 977 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 978 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 979 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 980 | return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 981 | RecursionLimit); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 984 | Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 985 | const DataLayout &DL, |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 986 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 987 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 988 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 989 | return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 990 | RecursionLimit); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 993 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 994 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 995 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 996 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 997 | return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 998 | RecursionLimit); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1001 | /// Given operands for an SDiv or UDiv, see if we can fold the result. |
| 1002 | /// If not, this returns null. |
Anders Carlsson | 36c6d23 | 2011-02-05 18:33:43 +0000 | [diff] [blame] | 1003 | static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1004 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1005 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 1006 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 1007 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1008 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1009 | bool isSigned = Opcode == Instruction::SDiv; |
| 1010 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1011 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1012 | if (match(Op1, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1013 | return Op1; |
| 1014 | |
David Majnemer | 71dc8fb | 2014-12-10 07:52:18 +0000 | [diff] [blame] | 1015 | // X / 0 -> undef, we don't need to preserve faults! |
| 1016 | if (match(Op1, m_Zero())) |
| 1017 | return UndefValue::get(Op1->getType()); |
| 1018 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1019 | // undef / X -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1020 | if (match(Op0, m_Undef())) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1021 | return Constant::getNullValue(Op0->getType()); |
| 1022 | |
| 1023 | // 0 / X -> 0, we don't need to preserve faults! |
| 1024 | if (match(Op0, m_Zero())) |
| 1025 | return Op0; |
| 1026 | |
| 1027 | // X / 1 -> X |
| 1028 | if (match(Op1, m_One())) |
| 1029 | return Op0; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (Op0->getType()->isIntegerTy(1)) |
| 1032 | // It can't be division by zero, hence it must be division by one. |
| 1033 | return Op0; |
| 1034 | |
| 1035 | // X / X -> 1 |
| 1036 | if (Op0 == Op1) |
| 1037 | return ConstantInt::get(Op0->getType(), 1); |
| 1038 | |
| 1039 | // (X * Y) / Y -> X if the multiplication does not overflow. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1040 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1041 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 1042 | 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] | 1043 | OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); |
Duncan Sands | 5747aba | 2011-02-02 20:52:00 +0000 | [diff] [blame] | 1044 | // If the Mul knows it does not overflow, then we are good to go. |
| 1045 | if ((isSigned && Mul->hasNoSignedWrap()) || |
| 1046 | (!isSigned && Mul->hasNoUnsignedWrap())) |
| 1047 | return X; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1048 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 1049 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 1050 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 1051 | return X; |
| 1052 | } |
| 1053 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1054 | // (X rem Y) / Y -> 0 |
| 1055 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1056 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 1057 | return Constant::getNullValue(Op0->getType()); |
| 1058 | |
David Majnemer | cb9d596 | 2014-10-11 10:20:01 +0000 | [diff] [blame] | 1059 | // (X /u C1) /u C2 -> 0 if C1 * C2 overflow |
| 1060 | ConstantInt *C1, *C2; |
| 1061 | if (!isSigned && match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1))) && |
| 1062 | match(Op1, m_ConstantInt(C2))) { |
| 1063 | bool Overflow; |
| 1064 | C1->getValue().umul_ov(C2->getValue(), Overflow); |
| 1065 | if (Overflow) |
| 1066 | return Constant::getNullValue(Op0->getType()); |
| 1067 | } |
| 1068 | |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1069 | // If the operation is with the result of a select instruction, check whether |
| 1070 | // operating on either branch of the select always yields the same value. |
| 1071 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1072 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1073 | return V; |
| 1074 | |
| 1075 | // If the operation is with the result of a phi instruction, check whether |
| 1076 | // operating on all incoming values of the phi always yields the same value. |
| 1077 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1078 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 65995fa | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 1079 | return V; |
| 1080 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1081 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1084 | /// Given operands for an SDiv, see if we can fold the result. |
| 1085 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1086 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1087 | unsigned MaxRecurse) { |
| 1088 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1089 | return V; |
| 1090 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1091 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1094 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1095 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1096 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1097 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1098 | return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1099 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1102 | /// Given operands for a UDiv, see if we can fold the result. |
| 1103 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1104 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, |
| 1105 | unsigned MaxRecurse) { |
| 1106 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1107 | return V; |
| 1108 | |
David Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 1109 | // udiv %V, C -> 0 if %V < C |
| 1110 | if (MaxRecurse) { |
| 1111 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1112 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1113 | if (C->isAllOnesValue()) { |
| 1114 | return Constant::getNullValue(Op0->getType()); |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1119 | return nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1122 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1123 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1124 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1125 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1126 | return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1127 | RecursionLimit); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1130 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1131 | const Query &Q, unsigned) { |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1132 | // undef / X -> undef (the undef could be a snan). |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1133 | if (match(Op0, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1134 | return Op0; |
| 1135 | |
| 1136 | // X / undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1137 | if (match(Op1, m_Undef())) |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1138 | return Op1; |
| 1139 | |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 1140 | // X / 1.0 -> X |
| 1141 | if (match(Op1, m_FPOne())) |
| 1142 | return Op0; |
| 1143 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1144 | // 0 / X -> 0 |
| 1145 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1146 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1147 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1148 | return Op0; |
| 1149 | |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1150 | if (FMF.noNaNs()) { |
| 1151 | // X / X -> 1.0 is legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1152 | if (Op0 == Op1) |
| 1153 | return ConstantFP::get(Op0->getType(), 1.0); |
| 1154 | |
| 1155 | // -X / X -> -1.0 and |
Benjamin Kramer | 1ee59cb | 2015-06-16 14:57:29 +0000 | [diff] [blame] | 1156 | // X / -X -> -1.0 are legal when NaNs are ignored. |
Benjamin Kramer | 4f05246 | 2015-06-14 18:53:58 +0000 | [diff] [blame] | 1157 | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
| 1158 | if ((BinaryOperator::isFNeg(Op0, /*IgnoreZeroSign=*/true) && |
| 1159 | BinaryOperator::getFNegArgument(Op0) == Op1) || |
| 1160 | (BinaryOperator::isFNeg(Op1, /*IgnoreZeroSign=*/true) && |
| 1161 | BinaryOperator::getFNegArgument(Op1) == Op0)) |
| 1162 | return ConstantFP::get(Op0->getType(), -1.0); |
| 1163 | } |
| 1164 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1165 | return nullptr; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1168 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1169 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1170 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1171 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1172 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1173 | return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1174 | RecursionLimit); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1177 | /// Given operands for an SRem or URem, see if we can fold the result. |
| 1178 | /// If not, this returns null. |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1179 | static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1180 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1181 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 1182 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 1183 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1184 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1185 | // X % undef -> undef |
| 1186 | if (match(Op1, m_Undef())) |
| 1187 | return Op1; |
| 1188 | |
| 1189 | // undef % X -> 0 |
| 1190 | if (match(Op0, m_Undef())) |
| 1191 | return Constant::getNullValue(Op0->getType()); |
| 1192 | |
| 1193 | // 0 % X -> 0, we don't need to preserve faults! |
| 1194 | if (match(Op0, m_Zero())) |
| 1195 | return Op0; |
| 1196 | |
| 1197 | // X % 0 -> undef, we don't need to preserve faults! |
| 1198 | if (match(Op1, m_Zero())) |
| 1199 | return UndefValue::get(Op0->getType()); |
| 1200 | |
| 1201 | // X % 1 -> 0 |
| 1202 | if (match(Op1, m_One())) |
| 1203 | return Constant::getNullValue(Op0->getType()); |
| 1204 | |
| 1205 | if (Op0->getType()->isIntegerTy(1)) |
| 1206 | // It can't be remainder by zero, hence it must be remainder by one. |
| 1207 | return Constant::getNullValue(Op0->getType()); |
| 1208 | |
| 1209 | // X % X -> 0 |
| 1210 | if (Op0 == Op1) |
| 1211 | return Constant::getNullValue(Op0->getType()); |
| 1212 | |
David Majnemer | b435a42 | 2014-09-17 04:16:35 +0000 | [diff] [blame] | 1213 | // (X % Y) % Y -> X % Y |
| 1214 | if ((Opcode == Instruction::SRem && |
| 1215 | match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 1216 | (Opcode == Instruction::URem && |
| 1217 | match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1218 | return Op0; |
David Majnemer | ac717f0 | 2014-09-17 03:34:34 +0000 | [diff] [blame] | 1219 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1220 | // If the operation is with the result of a select instruction, check whether |
| 1221 | // operating on either branch of the select always yields the same value. |
| 1222 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1223 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1224 | return V; |
| 1225 | |
| 1226 | // If the operation is with the result of a phi instruction, check whether |
| 1227 | // operating on all incoming values of the phi always yields the same value. |
| 1228 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1229 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1230 | return V; |
| 1231 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1232 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1235 | /// Given operands for an SRem, see if we can fold the result. |
| 1236 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1237 | static Value *SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, |
| 1238 | unsigned MaxRecurse) { |
| 1239 | if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1240 | return V; |
| 1241 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1242 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1245 | Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1246 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1247 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1248 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1249 | return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1250 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1253 | /// Given operands for a URem, see if we can fold the result. |
| 1254 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1255 | static Value *SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1256 | unsigned MaxRecurse) { |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1257 | if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1258 | return V; |
| 1259 | |
David Majnemer | 8c0e62f | 2017-01-06 21:23:51 +0000 | [diff] [blame] | 1260 | // urem %V, C -> %V if %V < C |
| 1261 | if (MaxRecurse) { |
| 1262 | if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst( |
| 1263 | ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) { |
| 1264 | if (C->isAllOnesValue()) { |
| 1265 | return Op0; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1270 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1273 | Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1274 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1275 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1276 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1277 | return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1278 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1281 | static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
| 1282 | const Query &, unsigned) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1283 | // undef % X -> undef (the undef could be a snan). |
| 1284 | if (match(Op0, m_Undef())) |
| 1285 | return Op0; |
| 1286 | |
| 1287 | // X % undef -> undef |
| 1288 | if (match(Op1, m_Undef())) |
| 1289 | return Op1; |
| 1290 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1291 | // 0 % X -> 0 |
| 1292 | // Requires that NaNs are off (X could be zero) and signed zeroes are |
| 1293 | // ignored (X could be positive or negative, so the output sign is unknown). |
| 1294 | if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZero())) |
| 1295 | return Op0; |
| 1296 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1297 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 1300 | Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1301 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1302 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1303 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1304 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1305 | return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1306 | RecursionLimit); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1309 | /// Returns true if a shift by \c Amount always yields undef. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1310 | static bool isUndefShift(Value *Amount) { |
| 1311 | Constant *C = dyn_cast<Constant>(Amount); |
| 1312 | if (!C) |
| 1313 | return false; |
| 1314 | |
| 1315 | // X shift by undef -> undef because it may shift by the bitwidth. |
| 1316 | if (isa<UndefValue>(C)) |
| 1317 | return true; |
| 1318 | |
| 1319 | // Shifting by the bitwidth or more is undefined. |
| 1320 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1321 | if (CI->getValue().getLimitedValue() >= |
| 1322 | CI->getType()->getScalarSizeInBits()) |
| 1323 | return true; |
| 1324 | |
| 1325 | // If all lanes of a vector shift are undefined the whole shift is. |
| 1326 | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) { |
| 1327 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; ++I) |
| 1328 | if (!isUndefShift(C->getAggregateElement(I))) |
| 1329 | return false; |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1336 | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
| 1337 | /// If not, this returns null. |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1338 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1339 | const Query &Q, unsigned MaxRecurse) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1340 | if (Constant *C0 = dyn_cast<Constant>(Op0)) |
| 1341 | if (Constant *C1 = dyn_cast<Constant>(Op1)) |
| 1342 | return ConstantFoldBinaryOpOperands(Opcode, C0, C1, Q.DL); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1343 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1344 | // 0 shift by X -> 0 |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1345 | if (match(Op0, m_Zero())) |
| 1346 | return Op0; |
| 1347 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1348 | // X shift by 0 -> X |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1349 | if (match(Op1, m_Zero())) |
| 1350 | return Op0; |
| 1351 | |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 1352 | // Fold undefined shifts. |
| 1353 | if (isUndefShift(Op1)) |
| 1354 | return UndefValue::get(Op0->getType()); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1355 | |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1356 | // If the operation is with the result of a select instruction, check whether |
| 1357 | // operating on either branch of the select always yields the same value. |
| 1358 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1359 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1360 | return V; |
| 1361 | |
| 1362 | // If the operation is with the result of a phi instruction, check whether |
| 1363 | // operating on all incoming values of the phi always yields the same value. |
| 1364 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1365 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1366 | return V; |
| 1367 | |
Sanjay Patel | 6786bc5 | 2016-05-10 20:46:54 +0000 | [diff] [blame] | 1368 | // If any bits in the shift amount make that value greater than or equal to |
| 1369 | // the number of bits in the type, the shift is undefined. |
| 1370 | unsigned BitWidth = Op1->getType()->getScalarSizeInBits(); |
| 1371 | APInt KnownZero(BitWidth, 0); |
| 1372 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1373 | 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] | 1374 | if (KnownOne.getLimitedValue() >= BitWidth) |
| 1375 | return UndefValue::get(Op0->getType()); |
| 1376 | |
| 1377 | // If all valid bits in the shift amount are known zero, the first operand is |
| 1378 | // unchanged. |
| 1379 | unsigned NumValidShiftBits = Log2_32_Ceil(BitWidth); |
| 1380 | APInt ShiftAmountMask = APInt::getLowBitsSet(BitWidth, NumValidShiftBits); |
| 1381 | if ((KnownZero & ShiftAmountMask) == ShiftAmountMask) |
| 1382 | return Op0; |
| 1383 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1384 | return nullptr; |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1387 | /// \brief Given operands for an Shl, LShr or AShr, see if we can |
| 1388 | /// fold the result. If not, this returns null. |
| 1389 | static Value *SimplifyRightShift(unsigned Opcode, Value *Op0, Value *Op1, |
| 1390 | bool isExact, const Query &Q, |
| 1391 | unsigned MaxRecurse) { |
| 1392 | if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse)) |
| 1393 | return V; |
| 1394 | |
| 1395 | // X >> X -> 0 |
| 1396 | if (Op0 == Op1) |
| 1397 | return Constant::getNullValue(Op0->getType()); |
| 1398 | |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1399 | // undef >> X -> 0 |
| 1400 | // undef >> X -> undef (if it's exact) |
| 1401 | if (match(Op0, m_Undef())) |
| 1402 | return isExact ? Op0 : Constant::getNullValue(Op0->getType()); |
| 1403 | |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1404 | // The low bit cannot be shifted out of an exact shift if it is set. |
| 1405 | if (isExact) { |
| 1406 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 1407 | APInt Op0KnownZero(BitWidth, 0); |
| 1408 | APInt Op0KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1409 | computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC, |
| 1410 | Q.CxtI, Q.DT); |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1411 | if (Op0KnownOne[0]) |
| 1412 | return Op0; |
| 1413 | } |
| 1414 | |
| 1415 | return nullptr; |
| 1416 | } |
| 1417 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1418 | /// Given operands for an Shl, see if we can fold the result. |
| 1419 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1420 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1421 | const Query &Q, unsigned MaxRecurse) { |
| 1422 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1423 | return V; |
| 1424 | |
| 1425 | // undef << X -> 0 |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1426 | // undef << X -> undef if (if it's NSW/NUW) |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1427 | if (match(Op0, m_Undef())) |
David Majnemer | 65c52ae | 2014-12-17 01:54:33 +0000 | [diff] [blame] | 1428 | return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType()); |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1430 | // (X >> A) << A -> X |
| 1431 | Value *X; |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1432 | 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] | 1433 | return X; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1434 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1437 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1438 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1439 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1440 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1441 | return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1442 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1445 | /// Given operands for an LShr, see if we can fold the result. |
| 1446 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1447 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1448 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1449 | if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, |
| 1450 | MaxRecurse)) |
| 1451 | return V; |
David Majnemer | a80fed7 | 2013-07-09 22:01:22 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1453 | // (X << A) >> A -> X |
| 1454 | Value *X; |
David Majnemer | 4f43837 | 2014-11-04 17:38:50 +0000 | [diff] [blame] | 1455 | if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1456 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1457 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1458 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1461 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1462 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1463 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1464 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1465 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1466 | return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1467 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1470 | /// Given operands for an AShr, see if we can fold the result. |
| 1471 | /// If not, this returns null. |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1472 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1473 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | bf7550e | 2014-11-05 00:59:59 +0000 | [diff] [blame] | 1474 | if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q, |
| 1475 | MaxRecurse)) |
Duncan Sands | 571fd9a | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 1476 | return V; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1477 | |
| 1478 | // all ones >>a X -> all ones |
| 1479 | if (match(Op0, m_AllOnes())) |
| 1480 | return Op0; |
| 1481 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1482 | // (X << A) >> A -> X |
| 1483 | Value *X; |
David Majnemer | 2de97fc | 2014-11-04 17:47:13 +0000 | [diff] [blame] | 1484 | if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1485 | return X; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 1486 | |
Suyog Sarda | 6886241 | 2014-07-17 06:28:15 +0000 | [diff] [blame] | 1487 | // Arithmetic shifting an all-sign-bit value is a no-op. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1488 | 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] | 1489 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 1490 | return Op0; |
| 1491 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1492 | return nullptr; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1495 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1496 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1497 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1498 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1499 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1500 | return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1501 | RecursionLimit); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1504 | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
| 1505 | ICmpInst *UnsignedICmp, bool IsAnd) { |
| 1506 | Value *X, *Y; |
| 1507 | |
| 1508 | ICmpInst::Predicate EqPred; |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1509 | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
| 1510 | !ICmpInst::isEquality(EqPred)) |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1511 | return nullptr; |
| 1512 | |
| 1513 | ICmpInst::Predicate UnsignedPred; |
| 1514 | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
| 1515 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1516 | ; |
| 1517 | else if (match(UnsignedICmp, |
| 1518 | m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) && |
| 1519 | ICmpInst::isUnsigned(UnsignedPred)) |
| 1520 | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
| 1521 | else |
| 1522 | return nullptr; |
| 1523 | |
| 1524 | // X < Y && Y != 0 --> X < Y |
| 1525 | // X < Y || Y != 0 --> Y != 0 |
| 1526 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE) |
| 1527 | return IsAnd ? UnsignedICmp : ZeroICmp; |
| 1528 | |
| 1529 | // X >= Y || Y != 0 --> true |
| 1530 | // X >= Y || Y == 0 --> X >= Y |
| 1531 | if (UnsignedPred == ICmpInst::ICMP_UGE && !IsAnd) { |
| 1532 | if (EqPred == ICmpInst::ICMP_NE) |
| 1533 | return getTrue(UnsignedICmp->getType()); |
| 1534 | return UnsignedICmp; |
| 1535 | } |
| 1536 | |
David Majnemer | d5b3aa4 | 2014-12-08 18:30:43 +0000 | [diff] [blame] | 1537 | // X < Y && Y == 0 --> false |
| 1538 | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ && |
| 1539 | IsAnd) |
| 1540 | return getFalse(UnsignedICmp->getType()); |
| 1541 | |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1542 | return nullptr; |
| 1543 | } |
| 1544 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1545 | /// Commuted variants are assumed to be handled by calling this function again |
| 1546 | /// with the parameters swapped. |
| 1547 | static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1548 | ICmpInst::Predicate Pred0, Pred1; |
| 1549 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1550 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1551 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1552 | return nullptr; |
| 1553 | |
| 1554 | // We have (icmp Pred0, A, B) & (icmp Pred1, A, B). |
| 1555 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1556 | // can eliminate Op1 from this 'and'. |
| 1557 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1558 | return Op0; |
| 1559 | |
| 1560 | // Check for any combination of predicates that are guaranteed to be disjoint. |
| 1561 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1562 | (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) || |
| 1563 | (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) || |
| 1564 | (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)) |
| 1565 | return getFalse(Op0->getType()); |
| 1566 | |
| 1567 | return nullptr; |
| 1568 | } |
| 1569 | |
| 1570 | /// Commuted variants are assumed to be handled by calling this function again |
| 1571 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1572 | static Value *SimplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1573 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true)) |
| 1574 | return X; |
| 1575 | |
Sanjay Patel | 9b1b2de | 2016-12-06 19:05:46 +0000 | [diff] [blame] | 1576 | if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1)) |
| 1577 | return X; |
| 1578 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1579 | // Look for this pattern: (icmp V, C0) & (icmp V, C1)). |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1580 | Type *ITy = Op0->getType(); |
| 1581 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1582 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1583 | Value *V; |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1584 | if (match(Op0, m_ICmp(Pred0, m_Value(V), m_APInt(C0))) && |
| 1585 | match(Op1, m_ICmp(Pred1, m_Specific(V), m_APInt(C1)))) { |
| 1586 | // Make a constant range that's the intersection of the two icmp ranges. |
| 1587 | // If the intersection is empty, we know that the result is false. |
| 1588 | auto Range0 = ConstantRange::makeAllowedICmpRegion(Pred0, *C0); |
| 1589 | auto Range1 = ConstantRange::makeAllowedICmpRegion(Pred1, *C1); |
| 1590 | if (Range0.intersectWith(Range1).isEmptySet()) |
| 1591 | return getFalse(ITy); |
| 1592 | } |
| 1593 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1594 | // (icmp (add V, C0), C1) & (icmp V, C0) |
| 1595 | 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] | 1596 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1597 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1598 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1599 | return nullptr; |
| 1600 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1601 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1602 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
| 1603 | return nullptr; |
| 1604 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1605 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1606 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1607 | |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1608 | const APInt Delta = *C1 - *C0; |
| 1609 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1610 | if (Delta == 2) { |
| 1611 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
| 1612 | return getFalse(ITy); |
| 1613 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1614 | return getFalse(ITy); |
| 1615 | } |
| 1616 | if (Delta == 1) { |
| 1617 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
| 1618 | return getFalse(ITy); |
| 1619 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW) |
| 1620 | return getFalse(ITy); |
| 1621 | } |
| 1622 | } |
Sanjay Patel | 1b312ad | 2016-09-28 13:53:13 +0000 | [diff] [blame] | 1623 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1624 | if (Delta == 2) |
| 1625 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
| 1626 | return getFalse(ITy); |
| 1627 | if (Delta == 1) |
| 1628 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
| 1629 | return getFalse(ITy); |
| 1630 | } |
| 1631 | |
| 1632 | return nullptr; |
| 1633 | } |
| 1634 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1635 | /// Given operands for an And, see if we can fold the result. |
| 1636 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1637 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const Query &Q, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1638 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1639 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1640 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1641 | return ConstantFoldBinaryOpOperands(Instruction::And, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1642 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1643 | // Canonicalize the constant to the RHS. |
| 1644 | std::swap(Op0, Op1); |
| 1645 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1646 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1647 | // X & undef -> 0 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1648 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1649 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1651 | // X & X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1652 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1653 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1654 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1655 | // X & 0 = 0 |
| 1656 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1657 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1658 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1659 | // X & -1 = X |
| 1660 | if (match(Op1, m_AllOnes())) |
| 1661 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1663 | // A & ~A = ~A & A = 0 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1664 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1665 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1666 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1667 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1668 | // (A | ?) & A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1669 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1670 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1671 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1672 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1674 | // A & (A | ?) = A |
| 1675 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1676 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1677 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1678 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1679 | // A & (-A) = A if A is a power of two or zero. |
| 1680 | if (match(Op0, m_Neg(m_Specific(Op1))) || |
| 1681 | match(Op1, m_Neg(m_Specific(Op0)))) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1682 | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1683 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1684 | return Op0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1685 | if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, |
| 1686 | Q.DT)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1687 | return Op1; |
| 1688 | } |
| 1689 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1690 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1691 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1692 | if (Value *V = SimplifyAndOfICmps(ICILHS, ICIRHS)) |
| 1693 | return V; |
| 1694 | if (Value *V = SimplifyAndOfICmps(ICIRHS, ICILHS)) |
| 1695 | return V; |
| 1696 | } |
| 1697 | } |
| 1698 | |
Sanjay Patel | 9ad8fb6 | 2016-06-20 20:59:59 +0000 | [diff] [blame] | 1699 | // The compares may be hidden behind casts. Look through those and try the |
| 1700 | // same folds as above. |
| 1701 | auto *Cast0 = dyn_cast<CastInst>(Op0); |
| 1702 | auto *Cast1 = dyn_cast<CastInst>(Op1); |
| 1703 | if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() && |
| 1704 | Cast0->getSrcTy() == Cast1->getSrcTy()) { |
| 1705 | auto *Cmp0 = dyn_cast<ICmpInst>(Cast0->getOperand(0)); |
| 1706 | auto *Cmp1 = dyn_cast<ICmpInst>(Cast1->getOperand(0)); |
| 1707 | if (Cmp0 && Cmp1) { |
| 1708 | Instruction::CastOps CastOpc = Cast0->getOpcode(); |
| 1709 | Type *ResultType = Cast0->getType(); |
| 1710 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp0, Cmp1))) |
| 1711 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1712 | if (auto *V = dyn_cast_or_null<Constant>(SimplifyAndOfICmps(Cmp1, Cmp0))) |
| 1713 | return ConstantExpr::getCast(CastOpc, V, ResultType); |
| 1714 | } |
| 1715 | } |
| 1716 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1717 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1718 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, |
| 1719 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1720 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1721 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1722 | // And distributes over Or. Try some generic simplifications based on this. |
| 1723 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1724 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1725 | return V; |
| 1726 | |
| 1727 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1728 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1729 | Q, MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1730 | return V; |
| 1731 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1732 | // If the operation is with the result of a select instruction, check whether |
| 1733 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1734 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1735 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, |
| 1736 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1737 | return V; |
| 1738 | |
| 1739 | // If the operation is with the result of a phi instruction, check whether |
| 1740 | // 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] | 1741 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1742 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1743 | MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1744 | return V; |
| 1745 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1746 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1749 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1750 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1751 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1752 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1753 | return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1754 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1757 | /// Commuted variants are assumed to be handled by calling this function again |
| 1758 | /// with the parameters swapped. |
| 1759 | static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { |
| 1760 | ICmpInst::Predicate Pred0, Pred1; |
| 1761 | Value *A ,*B; |
Sanjay Patel | 5369775 | 2016-12-06 22:09:52 +0000 | [diff] [blame] | 1762 | if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || |
| 1763 | !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1764 | return nullptr; |
| 1765 | |
| 1766 | // We have (icmp Pred0, A, B) | (icmp Pred1, A, B). |
| 1767 | // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we |
| 1768 | // can eliminate Op0 from this 'or'. |
| 1769 | if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1)) |
| 1770 | return Op1; |
| 1771 | |
| 1772 | // Check for any combination of predicates that cover the entire range of |
| 1773 | // possibilities. |
| 1774 | if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) || |
| 1775 | (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) || |
| 1776 | (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) || |
| 1777 | (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE)) |
| 1778 | return getTrue(Op0->getType()); |
| 1779 | |
| 1780 | return nullptr; |
| 1781 | } |
| 1782 | |
| 1783 | /// Commuted variants are assumed to be handled by calling this function again |
| 1784 | /// with the parameters swapped. |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1785 | static Value *SimplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1) { |
David Majnemer | 1af36e5 | 2014-12-06 10:51:40 +0000 | [diff] [blame] | 1786 | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false)) |
| 1787 | return X; |
| 1788 | |
Sanjay Patel | d0ccdb4 | 2016-12-06 18:09:37 +0000 | [diff] [blame] | 1789 | if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1)) |
| 1790 | return X; |
| 1791 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1792 | // (icmp (add V, C0), C1) | (icmp V, C0) |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1793 | ICmpInst::Predicate Pred0, Pred1; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1794 | const APInt *C0, *C1; |
Sanjay Patel | b2332e1 | 2016-09-20 14:36:14 +0000 | [diff] [blame] | 1795 | Value *V; |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1796 | 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] | 1797 | return nullptr; |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1798 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1799 | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
| 1800 | return nullptr; |
| 1801 | |
| 1802 | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
| 1803 | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1804 | return nullptr; |
| 1805 | |
| 1806 | Type *ITy = Op0->getType(); |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1807 | bool isNSW = AddInst->hasNoSignedWrap(); |
| 1808 | bool isNUW = AddInst->hasNoUnsignedWrap(); |
| 1809 | |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1810 | const APInt Delta = *C1 - *C0; |
| 1811 | if (C0->isStrictlyPositive()) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1812 | if (Delta == 2) { |
| 1813 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
| 1814 | return getTrue(ITy); |
| 1815 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1816 | return getTrue(ITy); |
| 1817 | } |
| 1818 | if (Delta == 1) { |
| 1819 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE) |
| 1820 | return getTrue(ITy); |
| 1821 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW) |
| 1822 | return getTrue(ITy); |
| 1823 | } |
| 1824 | } |
Sanjay Patel | 220a873 | 2016-09-28 14:27:21 +0000 | [diff] [blame] | 1825 | if (C0->getBoolValue() && isNUW) { |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1826 | if (Delta == 2) |
| 1827 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
| 1828 | return getTrue(ITy); |
| 1829 | if (Delta == 1) |
| 1830 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
| 1831 | return getTrue(ITy); |
| 1832 | } |
| 1833 | |
| 1834 | return nullptr; |
| 1835 | } |
| 1836 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1837 | /// Given operands for an Or, see if we can fold the result. |
| 1838 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1839 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const Query &Q, |
| 1840 | unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1841 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1842 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1843 | return ConstantFoldBinaryOpOperands(Instruction::Or, CLHS, CRHS, Q.DL); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1844 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1845 | // Canonicalize the constant to the RHS. |
| 1846 | std::swap(Op0, Op1); |
| 1847 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1849 | // X | undef -> -1 |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1850 | if (match(Op1, m_Undef())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1851 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1853 | // X | X = X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1854 | if (Op0 == Op1) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1855 | return Op0; |
| 1856 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1857 | // X | 0 = X |
| 1858 | if (match(Op1, m_Zero())) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1859 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1860 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1861 | // X | -1 = -1 |
| 1862 | if (match(Op1, m_AllOnes())) |
| 1863 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1865 | // A | ~A = ~A | A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1866 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1867 | match(Op1, m_Not(m_Specific(Op0)))) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1868 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1870 | // (A & ?) | A = A |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1871 | Value *A = nullptr, *B = nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1872 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1873 | (A == Op1 || B == Op1)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1874 | return Op1; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1875 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1876 | // A | (A & ?) = A |
| 1877 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1878 | (A == Op0 || B == Op0)) |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1879 | return Op0; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1880 | |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 1881 | // ~(A & ?) | A = -1 |
| 1882 | if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1883 | (A == Op1 || B == Op1)) |
| 1884 | return Constant::getAllOnesValue(Op1->getType()); |
| 1885 | |
| 1886 | // A | ~(A & ?) = -1 |
| 1887 | if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) && |
| 1888 | (A == Op0 || B == Op0)) |
| 1889 | return Constant::getAllOnesValue(Op0->getType()); |
| 1890 | |
David Majnemer | a315bd8 | 2014-09-15 08:15:28 +0000 | [diff] [blame] | 1891 | if (auto *ICILHS = dyn_cast<ICmpInst>(Op0)) { |
| 1892 | if (auto *ICIRHS = dyn_cast<ICmpInst>(Op1)) { |
| 1893 | if (Value *V = SimplifyOrOfICmps(ICILHS, ICIRHS)) |
| 1894 | return V; |
| 1895 | if (Value *V = SimplifyOrOfICmps(ICIRHS, ICILHS)) |
| 1896 | return V; |
| 1897 | } |
| 1898 | } |
| 1899 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1900 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1901 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, |
| 1902 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1903 | return V; |
Benjamin Kramer | 8c35fb0 | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1904 | |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1905 | // Or distributes over And. Try some generic simplifications based on this. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1906 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q, |
| 1907 | MaxRecurse)) |
Duncan Sands | ee3ec6e | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1908 | return V; |
| 1909 | |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1910 | // If the operation is with the result of a select instruction, check whether |
| 1911 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1912 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1913 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1914 | MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1915 | return V; |
| 1916 | |
Nick Lewycky | 8561a49 | 2014-06-19 03:51:46 +0000 | [diff] [blame] | 1917 | // (A & C)|(B & D) |
| 1918 | Value *C = nullptr, *D = nullptr; |
| 1919 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 1920 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| 1921 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 1922 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| 1923 | if (C1 && C2 && (C1->getValue() == ~C2->getValue())) { |
| 1924 | // (A & C1)|(B & C2) |
| 1925 | // If we have: ((V + N) & C1) | (V & C2) |
| 1926 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 1927 | // replace with V+N. |
| 1928 | Value *V1, *V2; |
| 1929 | if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+ |
| 1930 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1931 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1932 | if (V1 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1933 | 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] | 1934 | return A; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1935 | if (V2 == B && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1936 | 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] | 1937 | return A; |
| 1938 | } |
| 1939 | // Or commutes, try both ways. |
| 1940 | if ((C1->getValue() & (C1->getValue() + 1)) == 0 && |
| 1941 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { |
| 1942 | // Add commutes, try both ways. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1943 | if (V1 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1944 | 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] | 1945 | return B; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1946 | if (V2 == A && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1947 | 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] | 1948 | return B; |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1953 | // If the operation is with the result of a phi instruction, check whether |
| 1954 | // 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] | 1955 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1956 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1957 | return V; |
| 1958 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1959 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1962 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 1963 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1964 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1965 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1966 | return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1967 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1968 | } |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1969 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 1970 | /// Given operands for a Xor, see if we can fold the result. |
| 1971 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 1972 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const Query &Q, |
| 1973 | unsigned MaxRecurse) { |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1974 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 1975 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) |
| 1976 | return ConstantFoldBinaryOpOperands(Instruction::Xor, CLHS, CRHS, Q.DL); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Canonicalize the constant to the RHS. |
| 1979 | std::swap(Op0, Op1); |
| 1980 | } |
| 1981 | |
| 1982 | // A ^ undef -> undef |
Duncan Sands | a29ea9a | 2011-02-01 09:06:20 +0000 | [diff] [blame] | 1983 | if (match(Op1, m_Undef())) |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1984 | return Op1; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1985 | |
| 1986 | // A ^ 0 = A |
| 1987 | if (match(Op1, m_Zero())) |
| 1988 | return Op0; |
| 1989 | |
Eli Friedman | ad3cfe7 | 2011-08-17 19:31:49 +0000 | [diff] [blame] | 1990 | // A ^ A = 0 |
| 1991 | if (Op0 == Op1) |
| 1992 | return Constant::getNullValue(Op0->getType()); |
| 1993 | |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1994 | // A ^ ~A = ~A ^ A = -1 |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 1995 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 1996 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1997 | return Constant::getAllOnesValue(Op0->getType()); |
| 1998 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1999 | // Try some generic simplifications for associative operations. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 2000 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, |
| 2001 | MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 2002 | return V; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2003 | |
Duncan Sands | b238de0 | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 2004 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 2005 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 2006 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 2007 | // only if B and C are equal. If B and C are equal then (since we assume |
| 2008 | // that operands have already been simplified) "select(cond, B, C)" should |
| 2009 | // have been simplified to the common value of B and C already. Analysing |
| 2010 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 2011 | // for threading over phi nodes. |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2012 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2013 | return nullptr; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2016 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 2017 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2018 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2019 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2020 | return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2021 | RecursionLimit); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2024 | static Type *GetCompareTy(Value *Op) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 2025 | return CmpInst::makeCmpResultType(Op->getType()); |
| 2026 | } |
| 2027 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 2028 | /// Rummage around inside V looking for something equivalent to the comparison |
| 2029 | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
| 2030 | /// Helper function for analyzing max/min idioms. |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2031 | static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, |
| 2032 | Value *LHS, Value *RHS) { |
| 2033 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 2034 | if (!SI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2035 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2036 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 2037 | if (!Cmp) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2038 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2039 | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
| 2040 | if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS) |
| 2041 | return Cmp; |
| 2042 | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
| 2043 | LHS == CmpRHS && RHS == CmpLHS) |
| 2044 | return Cmp; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2045 | return nullptr; |
Duncan Sands | af32728 | 2011-05-07 16:56:49 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2048 | // A significant optimization not implemented here is assuming that alloca |
| 2049 | // addresses are not equal to incoming argument values. They don't *alias*, |
| 2050 | // as we say, but that doesn't mean they aren't equal, so we take a |
| 2051 | // conservative approach. |
| 2052 | // |
| 2053 | // This is inspired in part by C++11 5.10p1: |
| 2054 | // "Two pointers of the same type compare equal if and only if they are both |
| 2055 | // null, both point to the same function, or both represent the same |
| 2056 | // address." |
| 2057 | // |
| 2058 | // This is pretty permissive. |
| 2059 | // |
| 2060 | // It's also partly due to C11 6.5.9p6: |
| 2061 | // "Two pointers compare equal if and only if both are null pointers, both are |
| 2062 | // pointers to the same object (including a pointer to an object and a |
| 2063 | // subobject at its beginning) or function, both are pointers to one past the |
| 2064 | // last element of the same array object, or one is a pointer to one past the |
| 2065 | // 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] | 2066 | // different array object that happens to immediately follow the first array |
Dan Gohman | 9631d90 | 2013-02-01 00:49:06 +0000 | [diff] [blame] | 2067 | // object in the address space.) |
| 2068 | // |
| 2069 | // C11's version is more restrictive, however there's no reason why an argument |
| 2070 | // couldn't be a one-past-the-end value for a stack object in the caller and be |
| 2071 | // equal to the beginning of a stack object in the callee. |
| 2072 | // |
| 2073 | // If the C and C++ standards are ever made sufficiently restrictive in this |
| 2074 | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
| 2075 | // this optimization. |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2076 | static Constant * |
| 2077 | computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 2078 | const DominatorTree *DT, CmpInst::Predicate Pred, |
| 2079 | const Instruction *CxtI, Value *LHS, Value *RHS) { |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2080 | // First, skip past any trivial no-ops. |
| 2081 | LHS = LHS->stripPointerCasts(); |
| 2082 | RHS = RHS->stripPointerCasts(); |
| 2083 | |
| 2084 | // A non-null pointer is not equal to a null pointer. |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2085 | if (llvm::isKnownNonNull(LHS) && isa<ConstantPointerNull>(RHS) && |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2086 | (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE)) |
| 2087 | return ConstantInt::get(GetCompareTy(LHS), |
| 2088 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2089 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2090 | // We can only fold certain predicates on pointer comparisons. |
| 2091 | switch (Pred) { |
| 2092 | default: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2093 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2094 | |
| 2095 | // Equality comaprisons are easy to fold. |
| 2096 | case CmpInst::ICMP_EQ: |
| 2097 | case CmpInst::ICMP_NE: |
| 2098 | break; |
| 2099 | |
| 2100 | // We can only handle unsigned relational comparisons because 'inbounds' on |
| 2101 | // a GEP only protects against unsigned wrapping. |
| 2102 | case CmpInst::ICMP_UGT: |
| 2103 | case CmpInst::ICMP_UGE: |
| 2104 | case CmpInst::ICMP_ULT: |
| 2105 | case CmpInst::ICMP_ULE: |
| 2106 | // However, we have to switch them to their signed variants to handle |
| 2107 | // negative indices from the base pointer. |
| 2108 | Pred = ICmpInst::getSignedPredicate(Pred); |
| 2109 | break; |
| 2110 | } |
| 2111 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2112 | // Strip off any constant offsets so that we can reason about them. |
| 2113 | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
| 2114 | // here and compare base addresses like AliasAnalysis does, however there are |
| 2115 | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
| 2116 | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
| 2117 | // doesn't need to guarantee pointer inequality when it says NoAlias. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2118 | Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
| 2119 | Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2120 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2121 | // If LHS and RHS are related via constant offsets to the same base |
| 2122 | // value, we can replace it with an icmp which just compares the offsets. |
| 2123 | if (LHS == RHS) |
| 2124 | return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset); |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2125 | |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2126 | // Various optimizations for (in)equality comparisons. |
| 2127 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { |
| 2128 | // Different non-empty allocations that exist at the same time have |
| 2129 | // different addresses (if the program can tell). Global variables always |
| 2130 | // exist, so they always exist during the lifetime of each other and all |
| 2131 | // allocas. Two different allocas usually have different addresses... |
| 2132 | // |
| 2133 | // However, if there's an @llvm.stackrestore dynamically in between two |
| 2134 | // allocas, they may have the same address. It's tempting to reduce the |
| 2135 | // scope of the problem by only looking at *static* allocas here. That would |
| 2136 | // cover the majority of allocas while significantly reducing the likelihood |
| 2137 | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
| 2138 | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
| 2139 | // an entry block. Also, if we have a block that's not attached to a |
| 2140 | // function, we can't tell if it's "static" under the current definition. |
| 2141 | // Theoretically, this problem could be fixed by creating a new kind of |
| 2142 | // instruction kind specifically for static allocas. Such a new instruction |
| 2143 | // could be required to be at the top of the entry block, thus preventing it |
| 2144 | // from being subject to a @llvm.stackrestore. Instcombine could even |
| 2145 | // convert regular allocas into these special allocas. It'd be nifty. |
| 2146 | // However, until then, this problem remains open. |
| 2147 | // |
| 2148 | // So, we'll assume that two non-empty allocas have different addresses |
| 2149 | // for now. |
| 2150 | // |
| 2151 | // With all that, if the offsets are within the bounds of their allocations |
| 2152 | // (and not one-past-the-end! so we can't use inbounds!), and their |
| 2153 | // allocations aren't the same, the pointers are not equal. |
| 2154 | // |
| 2155 | // Note that it's not necessary to check for LHS being a global variable |
| 2156 | // address, due to canonicalization and constant folding. |
| 2157 | if (isa<AllocaInst>(LHS) && |
| 2158 | (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2159 | ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset); |
| 2160 | ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2161 | uint64_t LHSSize, RHSSize; |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2162 | if (LHSOffsetCI && RHSOffsetCI && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2163 | getObjectSize(LHS, LHSSize, DL, TLI) && |
| 2164 | getObjectSize(RHS, RHSSize, DL, TLI)) { |
Benjamin Kramer | c05aa95 | 2013-02-01 15:21:10 +0000 | [diff] [blame] | 2165 | const APInt &LHSOffsetValue = LHSOffsetCI->getValue(); |
| 2166 | const APInt &RHSOffsetValue = RHSOffsetCI->getValue(); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2167 | if (!LHSOffsetValue.isNegative() && |
| 2168 | !RHSOffsetValue.isNegative() && |
| 2169 | LHSOffsetValue.ult(LHSSize) && |
| 2170 | RHSOffsetValue.ult(RHSSize)) { |
| 2171 | return ConstantInt::get(GetCompareTy(LHS), |
| 2172 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | // Repeat the above check but this time without depending on DataLayout |
| 2177 | // or being able to compute a precise size. |
| 2178 | if (!cast<PointerType>(LHS->getType())->isEmptyTy() && |
| 2179 | !cast<PointerType>(RHS->getType())->isEmptyTy() && |
| 2180 | LHSOffset->isNullValue() && |
| 2181 | RHSOffset->isNullValue()) |
| 2182 | return ConstantInt::get(GetCompareTy(LHS), |
| 2183 | !CmpInst::isTrueWhenEqual(Pred)); |
| 2184 | } |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Even if an non-inbounds GEP occurs along the path we can still optimize |
| 2187 | // equality comparisons concerning the result. We avoid walking the whole |
| 2188 | // chain again by starting where the last calls to |
| 2189 | // stripAndComputeConstantOffsets left off and accumulate the offsets. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2190 | Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true); |
| 2191 | Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true); |
Benjamin Kramer | 942dfe6 | 2013-09-23 14:16:38 +0000 | [diff] [blame] | 2192 | if (LHS == RHS) |
| 2193 | return ConstantExpr::getICmp(Pred, |
| 2194 | ConstantExpr::getAdd(LHSOffset, LHSNoBound), |
| 2195 | ConstantExpr::getAdd(RHSOffset, RHSNoBound)); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2196 | |
| 2197 | // If one side of the equality comparison must come from a noalias call |
| 2198 | // (meaning a system memory allocation function), and the other side must |
| 2199 | // come from a pointer that cannot overlap with dynamically-allocated |
| 2200 | // memory within the lifetime of the current function (allocas, byval |
| 2201 | // arguments, globals), then determine the comparison result here. |
| 2202 | SmallVector<Value *, 8> LHSUObjs, RHSUObjs; |
| 2203 | GetUnderlyingObjects(LHS, LHSUObjs, DL); |
| 2204 | GetUnderlyingObjects(RHS, RHSUObjs, DL); |
| 2205 | |
| 2206 | // Is the set of underlying objects all noalias calls? |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2207 | auto IsNAC = [](ArrayRef<Value *> Objects) { |
| 2208 | return all_of(Objects, isNoAliasCall); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2209 | }; |
| 2210 | |
| 2211 | // 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] | 2212 | // noalias calls. For allocas, we consider only static ones (dynamic |
| 2213 | // allocas might be transformed into calls to malloc not simultaneously |
| 2214 | // live with the compared-to allocation). For globals, we exclude symbols |
| 2215 | // that might be resolve lazily to symbols in another dynamically-loaded |
| 2216 | // library (and, thus, could be malloc'ed by the implementation). |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2217 | auto IsAllocDisjoint = [](ArrayRef<Value *> Objects) { |
| 2218 | return all_of(Objects, [](Value *V) { |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2219 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 2220 | return AI->getParent() && AI->getFunction() && AI->isStaticAlloca(); |
| 2221 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 2222 | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 2223 | GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && |
Sanjay Patel | 34ea70a | 2016-01-11 22:24:35 +0000 | [diff] [blame] | 2224 | !GV->isThreadLocal(); |
| 2225 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2226 | return A->hasByValAttr(); |
| 2227 | return false; |
| 2228 | }); |
Hal Finkel | afcd8db | 2014-12-01 23:38:06 +0000 | [diff] [blame] | 2229 | }; |
| 2230 | |
| 2231 | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || |
| 2232 | (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) |
| 2233 | return ConstantInt::get(GetCompareTy(LHS), |
| 2234 | !CmpInst::isTrueWhenEqual(Pred)); |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2235 | |
| 2236 | // Fold comparisons for non-escaping pointer even if the allocation call |
| 2237 | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
| 2238 | // dynamic allocation call could be either of the operands. |
| 2239 | Value *MI = nullptr; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2240 | if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonNullAt(RHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2241 | MI = LHS; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 2242 | else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonNullAt(LHS, CxtI, DT)) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 2243 | MI = RHS; |
| 2244 | // FIXME: We should also fold the compare when the pointer escapes, but the |
| 2245 | // compare dominates the pointer escape |
| 2246 | if (MI && !PointerMayBeCaptured(MI, true, true)) |
| 2247 | return ConstantInt::get(GetCompareTy(LHS), |
| 2248 | CmpInst::isFalseWhenEqual(Pred)); |
Dan Gohman | b3e2d3a | 2013-02-01 00:11:13 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | // Otherwise, fail. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2252 | return nullptr; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 2253 | } |
Chris Lattner | 01990f0 | 2012-02-24 19:01:58 +0000 | [diff] [blame] | 2254 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2255 | /// Fold an icmp when its operands have i1 scalar type. |
| 2256 | static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS, |
| 2257 | Value *RHS, const Query &Q) { |
| 2258 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2259 | Type *OpTy = LHS->getType(); // The operand type. |
| 2260 | if (!OpTy->getScalarType()->isIntegerTy(1)) |
| 2261 | return nullptr; |
| 2262 | |
| 2263 | switch (Pred) { |
| 2264 | default: |
| 2265 | break; |
| 2266 | case ICmpInst::ICMP_EQ: |
| 2267 | // X == 1 -> X |
| 2268 | if (match(RHS, m_One())) |
| 2269 | return LHS; |
| 2270 | break; |
| 2271 | case ICmpInst::ICMP_NE: |
| 2272 | // X != 0 -> X |
| 2273 | if (match(RHS, m_Zero())) |
| 2274 | return LHS; |
| 2275 | break; |
| 2276 | case ICmpInst::ICMP_UGT: |
| 2277 | // X >u 0 -> X |
| 2278 | if (match(RHS, m_Zero())) |
| 2279 | return LHS; |
| 2280 | break; |
| 2281 | case ICmpInst::ICMP_UGE: |
| 2282 | // X >=u 1 -> X |
| 2283 | if (match(RHS, m_One())) |
| 2284 | return LHS; |
| 2285 | if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false)) |
| 2286 | return getTrue(ITy); |
| 2287 | break; |
| 2288 | case ICmpInst::ICMP_SGE: |
| 2289 | /// For signed comparison, the values for an i1 are 0 and -1 |
| 2290 | /// respectively. This maps into a truth table of: |
| 2291 | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
| 2292 | /// 0 | 0 | 1 (0 >= 0) | 1 |
| 2293 | /// 0 | 1 | 1 (0 >= -1) | 1 |
| 2294 | /// 1 | 0 | 0 (-1 >= 0) | 0 |
| 2295 | /// 1 | 1 | 1 (-1 >= -1) | 1 |
| 2296 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2297 | return getTrue(ITy); |
| 2298 | break; |
| 2299 | case ICmpInst::ICMP_SLT: |
| 2300 | // X <s 0 -> X |
| 2301 | if (match(RHS, m_Zero())) |
| 2302 | return LHS; |
| 2303 | break; |
| 2304 | case ICmpInst::ICMP_SLE: |
| 2305 | // X <=s -1 -> X |
| 2306 | if (match(RHS, m_One())) |
| 2307 | return LHS; |
| 2308 | break; |
| 2309 | case ICmpInst::ICMP_ULE: |
| 2310 | if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false)) |
| 2311 | return getTrue(ITy); |
| 2312 | break; |
| 2313 | } |
| 2314 | |
| 2315 | return nullptr; |
| 2316 | } |
| 2317 | |
| 2318 | /// Try hard to fold icmp with zero RHS because this is a common case. |
| 2319 | static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS, |
| 2320 | Value *RHS, const Query &Q) { |
| 2321 | if (!match(RHS, m_Zero())) |
| 2322 | return nullptr; |
| 2323 | |
| 2324 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2325 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2326 | switch (Pred) { |
| 2327 | default: |
| 2328 | llvm_unreachable("Unknown ICmp predicate!"); |
| 2329 | case ICmpInst::ICMP_ULT: |
| 2330 | return getFalse(ITy); |
| 2331 | case ICmpInst::ICMP_UGE: |
| 2332 | return getTrue(ITy); |
| 2333 | case ICmpInst::ICMP_EQ: |
| 2334 | case ICmpInst::ICMP_ULE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2335 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2336 | return getFalse(ITy); |
| 2337 | break; |
| 2338 | case ICmpInst::ICMP_NE: |
| 2339 | case ICmpInst::ICMP_UGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2340 | if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2341 | return getTrue(ITy); |
| 2342 | break; |
| 2343 | case ICmpInst::ICMP_SLT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2344 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2345 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2346 | if (LHSKnownNegative) |
| 2347 | return getTrue(ITy); |
| 2348 | if (LHSKnownNonNegative) |
| 2349 | return getFalse(ITy); |
| 2350 | break; |
| 2351 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2352 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2353 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2354 | if (LHSKnownNegative) |
| 2355 | return getTrue(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2356 | 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] | 2357 | return getFalse(ITy); |
| 2358 | break; |
| 2359 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2360 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2361 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2362 | if (LHSKnownNegative) |
| 2363 | return getFalse(ITy); |
| 2364 | if (LHSKnownNonNegative) |
| 2365 | return getTrue(ITy); |
| 2366 | break; |
| 2367 | case ICmpInst::ICMP_SGT: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2368 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC, |
| 2369 | Q.CxtI, Q.DT); |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 2370 | if (LHSKnownNegative) |
| 2371 | return getFalse(ITy); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2372 | 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] | 2373 | return getTrue(ITy); |
| 2374 | break; |
| 2375 | } |
| 2376 | |
| 2377 | return nullptr; |
| 2378 | } |
| 2379 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2380 | /// Many binary operators with a constant operand have an easy-to-compute |
| 2381 | /// range of outputs. This can be used to fold a comparison to always true or |
| 2382 | /// always false. |
| 2383 | static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper) { |
| 2384 | unsigned Width = Lower.getBitWidth(); |
| 2385 | const APInt *C; |
| 2386 | switch (BO.getOpcode()) { |
| 2387 | case Instruction::Add: |
Sanjay Patel | 5622725 | 2017-01-24 17:03:24 +0000 | [diff] [blame^] | 2388 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2389 | // FIXME: If we have both nuw and nsw, we should reduce the range further. |
| 2390 | if (BO.hasNoUnsignedWrap()) { |
| 2391 | // 'add nuw x, C' produces [C, UINT_MAX]. |
| 2392 | Lower = *C; |
| 2393 | } else if (BO.hasNoSignedWrap()) { |
| 2394 | if (C->isNegative()) { |
| 2395 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
| 2396 | Lower = APInt::getSignedMinValue(Width); |
| 2397 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
| 2398 | } else { |
| 2399 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
| 2400 | Lower = APInt::getSignedMinValue(Width) + *C; |
| 2401 | Upper = APInt::getSignedMaxValue(Width) + 1; |
| 2402 | } |
| 2403 | } |
| 2404 | } |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2405 | break; |
| 2406 | |
| 2407 | case Instruction::And: |
| 2408 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2409 | // 'and x, C' produces [0, C]. |
| 2410 | Upper = *C + 1; |
| 2411 | break; |
| 2412 | |
| 2413 | case Instruction::Or: |
| 2414 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2415 | // 'or x, C' produces [C, UINT_MAX]. |
| 2416 | Lower = *C; |
| 2417 | break; |
| 2418 | |
| 2419 | case Instruction::AShr: |
| 2420 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2421 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
| 2422 | Lower = APInt::getSignedMinValue(Width).ashr(*C); |
| 2423 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
| 2424 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2425 | unsigned ShiftAmount = Width - 1; |
| 2426 | if (*C != 0 && BO.isExact()) |
| 2427 | ShiftAmount = C->countTrailingZeros(); |
| 2428 | if (C->isNegative()) { |
| 2429 | // 'ashr C, x' produces [C, C >> (Width-1)] |
| 2430 | Lower = *C; |
| 2431 | Upper = C->ashr(ShiftAmount) + 1; |
| 2432 | } else { |
| 2433 | // 'ashr C, x' produces [C >> (Width-1), C] |
| 2434 | Lower = C->ashr(ShiftAmount); |
| 2435 | Upper = *C + 1; |
| 2436 | } |
| 2437 | } |
| 2438 | break; |
| 2439 | |
| 2440 | case Instruction::LShr: |
| 2441 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 2442 | // 'lshr x, C' produces [0, UINT_MAX >> C]. |
| 2443 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; |
| 2444 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2445 | // 'lshr C, x' produces [C >> (Width-1), C]. |
| 2446 | unsigned ShiftAmount = Width - 1; |
| 2447 | if (*C != 0 && BO.isExact()) |
| 2448 | ShiftAmount = C->countTrailingZeros(); |
| 2449 | Lower = C->lshr(ShiftAmount); |
| 2450 | Upper = *C + 1; |
| 2451 | } |
| 2452 | break; |
| 2453 | |
| 2454 | case Instruction::Shl: |
| 2455 | if (match(BO.getOperand(0), m_APInt(C))) { |
| 2456 | if (BO.hasNoUnsignedWrap()) { |
| 2457 | // 'shl nuw C, x' produces [C, C << CLZ(C)] |
| 2458 | Lower = *C; |
| 2459 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; |
| 2460 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
| 2461 | if (C->isNegative()) { |
| 2462 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] |
| 2463 | unsigned ShiftAmount = C->countLeadingOnes() - 1; |
| 2464 | Lower = C->shl(ShiftAmount); |
| 2465 | Upper = *C + 1; |
| 2466 | } else { |
| 2467 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
| 2468 | unsigned ShiftAmount = C->countLeadingZeros() - 1; |
| 2469 | Lower = *C; |
| 2470 | Upper = C->shl(ShiftAmount) + 1; |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | break; |
| 2475 | |
| 2476 | case Instruction::SDiv: |
| 2477 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2478 | APInt IntMin = APInt::getSignedMinValue(Width); |
| 2479 | APInt IntMax = APInt::getSignedMaxValue(Width); |
| 2480 | if (C->isAllOnesValue()) { |
| 2481 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 2482 | // where C != -1 and C != 0 and C != 1 |
| 2483 | Lower = IntMin + 1; |
| 2484 | Upper = IntMax + 1; |
| 2485 | } else if (C->countLeadingZeros() < Width - 1) { |
| 2486 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
| 2487 | // where C != -1 and C != 0 and C != 1 |
| 2488 | Lower = IntMin.sdiv(*C); |
| 2489 | Upper = IntMax.sdiv(*C); |
| 2490 | if (Lower.sgt(Upper)) |
| 2491 | std::swap(Lower, Upper); |
| 2492 | Upper = Upper + 1; |
| 2493 | assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 2494 | } |
| 2495 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2496 | if (C->isMinSignedValue()) { |
| 2497 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 2498 | Lower = *C; |
| 2499 | Upper = Lower.lshr(1) + 1; |
| 2500 | } else { |
| 2501 | // 'sdiv C, x' produces [-|C|, |C|]. |
| 2502 | Upper = C->abs() + 1; |
| 2503 | Lower = (-Upper) + 1; |
| 2504 | } |
| 2505 | } |
| 2506 | break; |
| 2507 | |
| 2508 | case Instruction::UDiv: |
| 2509 | if (match(BO.getOperand(1), m_APInt(C)) && *C != 0) { |
| 2510 | // 'udiv x, C' produces [0, UINT_MAX / C]. |
| 2511 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
| 2512 | } else if (match(BO.getOperand(0), m_APInt(C))) { |
| 2513 | // 'udiv C, x' produces [0, C]. |
| 2514 | Upper = *C + 1; |
| 2515 | } |
| 2516 | break; |
| 2517 | |
| 2518 | case Instruction::SRem: |
| 2519 | if (match(BO.getOperand(1), m_APInt(C))) { |
| 2520 | // 'srem x, C' produces (-|C|, |C|). |
| 2521 | Upper = C->abs(); |
| 2522 | Lower = (-Upper) + 1; |
| 2523 | } |
| 2524 | break; |
| 2525 | |
| 2526 | case Instruction::URem: |
| 2527 | if (match(BO.getOperand(1), m_APInt(C))) |
| 2528 | // 'urem x, C' produces [0, C). |
| 2529 | Upper = *C; |
| 2530 | break; |
| 2531 | |
| 2532 | default: |
| 2533 | break; |
| 2534 | } |
| 2535 | } |
| 2536 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2537 | static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, |
| 2538 | Value *RHS) { |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2539 | const APInt *C; |
| 2540 | if (!match(RHS, m_APInt(C))) |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2541 | return nullptr; |
| 2542 | |
| 2543 | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
Sanjoy Das | 1f7b813 | 2016-10-02 00:09:57 +0000 | [diff] [blame] | 2544 | ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2545 | if (RHS_CR.isEmptySet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2546 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2547 | if (RHS_CR.isFullSet()) |
Sanjay Patel | 200e3cb | 2016-08-23 17:30:56 +0000 | [diff] [blame] | 2548 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
| 2549 | |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2550 | // Find the range of possible values for binary operators. |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2551 | unsigned Width = C->getBitWidth(); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2552 | APInt Lower = APInt(Width, 0); |
| 2553 | APInt Upper = APInt(Width, 0); |
Sanjay Patel | be33213 | 2017-01-23 18:22:26 +0000 | [diff] [blame] | 2554 | if (auto *BO = dyn_cast<BinaryOperator>(LHS)) |
| 2555 | setLimitsForBinOp(*BO, Lower, Upper); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2556 | |
| 2557 | ConstantRange LHS_CR = |
| 2558 | Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true); |
| 2559 | |
| 2560 | if (auto *I = dyn_cast<Instruction>(LHS)) |
| 2561 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 2562 | LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges)); |
| 2563 | |
| 2564 | if (!LHS_CR.isFullSet()) { |
| 2565 | if (RHS_CR.contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2566 | return ConstantInt::getTrue(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2567 | if (RHS_CR.inverse().contains(LHS_CR)) |
Sanjay Patel | 6946e2a | 2016-08-23 18:00:51 +0000 | [diff] [blame] | 2568 | return ConstantInt::getFalse(GetCompareTy(RHS)); |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | return nullptr; |
| 2572 | } |
| 2573 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2574 | static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS, |
| 2575 | Value *RHS, const Query &Q, |
| 2576 | unsigned MaxRecurse) { |
| 2577 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2578 | |
| 2579 | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
| 2580 | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
| 2581 | if (MaxRecurse && (LBO || RBO)) { |
| 2582 | // Analyze the case when either LHS or RHS is an add instruction. |
| 2583 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
| 2584 | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
| 2585 | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
| 2586 | if (LBO && LBO->getOpcode() == Instruction::Add) { |
| 2587 | A = LBO->getOperand(0); |
| 2588 | B = LBO->getOperand(1); |
| 2589 | NoLHSWrapProblem = |
| 2590 | ICmpInst::isEquality(Pred) || |
| 2591 | (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) || |
| 2592 | (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap()); |
| 2593 | } |
| 2594 | if (RBO && RBO->getOpcode() == Instruction::Add) { |
| 2595 | C = RBO->getOperand(0); |
| 2596 | D = RBO->getOperand(1); |
| 2597 | NoRHSWrapProblem = |
| 2598 | ICmpInst::isEquality(Pred) || |
| 2599 | (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) || |
| 2600 | (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap()); |
| 2601 | } |
| 2602 | |
| 2603 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2604 | if ((A == RHS || B == RHS) && NoLHSWrapProblem) |
| 2605 | if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A, |
| 2606 | Constant::getNullValue(RHS->getType()), Q, |
| 2607 | MaxRecurse - 1)) |
| 2608 | return V; |
| 2609 | |
| 2610 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2611 | if ((C == LHS || D == LHS) && NoRHSWrapProblem) |
| 2612 | if (Value *V = |
| 2613 | SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()), |
| 2614 | C == LHS ? D : C, Q, MaxRecurse - 1)) |
| 2615 | return V; |
| 2616 | |
| 2617 | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
| 2618 | if (A && C && (A == C || A == D || B == C || B == D) && NoLHSWrapProblem && |
| 2619 | NoRHSWrapProblem) { |
| 2620 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2621 | Value *Y, *Z; |
| 2622 | if (A == C) { |
| 2623 | // C + B == C + D -> B == D |
| 2624 | Y = B; |
| 2625 | Z = D; |
| 2626 | } else if (A == D) { |
| 2627 | // D + B == C + D -> B == C |
| 2628 | Y = B; |
| 2629 | Z = C; |
| 2630 | } else if (B == C) { |
| 2631 | // A + C == C + D -> A == D |
| 2632 | Y = A; |
| 2633 | Z = D; |
| 2634 | } else { |
| 2635 | assert(B == D); |
| 2636 | // A + D == C + D -> A == C |
| 2637 | Y = A; |
| 2638 | Z = C; |
| 2639 | } |
| 2640 | if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1)) |
| 2641 | return V; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | { |
| 2646 | Value *Y = nullptr; |
| 2647 | // icmp pred (or X, Y), X |
| 2648 | if (LBO && match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
| 2649 | if (Pred == ICmpInst::ICMP_ULT) |
| 2650 | return getFalse(ITy); |
| 2651 | if (Pred == ICmpInst::ICMP_UGE) |
| 2652 | return getTrue(ITy); |
| 2653 | |
| 2654 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) { |
| 2655 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 2656 | bool YKnownNonNegative, YKnownNegative; |
| 2657 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2658 | Q.AC, Q.CxtI, Q.DT); |
| 2659 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2660 | Q.CxtI, Q.DT); |
| 2661 | if (RHSKnownNonNegative && YKnownNegative) |
| 2662 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
| 2663 | if (RHSKnownNegative || YKnownNonNegative) |
| 2664 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy); |
| 2665 | } |
| 2666 | } |
| 2667 | // icmp pred X, (or X, Y) |
| 2668 | if (RBO && match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))) { |
| 2669 | if (Pred == ICmpInst::ICMP_ULE) |
| 2670 | return getTrue(ITy); |
| 2671 | if (Pred == ICmpInst::ICMP_UGT) |
| 2672 | return getFalse(ITy); |
| 2673 | |
| 2674 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLE) { |
| 2675 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 2676 | bool YKnownNonNegative, YKnownNegative; |
| 2677 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2678 | Q.AC, Q.CxtI, Q.DT); |
| 2679 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC, |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2680 | Q.CxtI, Q.DT); |
| 2681 | if (LHSKnownNonNegative && YKnownNegative) |
| 2682 | return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy); |
| 2683 | if (LHSKnownNegative || YKnownNonNegative) |
| 2684 | return Pred == ICmpInst::ICMP_SGT ? getFalse(ITy) : getTrue(ITy); |
| 2685 | } |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | // icmp pred (and X, Y), X |
| 2690 | if (LBO && match(LBO, m_CombineOr(m_And(m_Value(), m_Specific(RHS)), |
| 2691 | m_And(m_Specific(RHS), m_Value())))) { |
| 2692 | if (Pred == ICmpInst::ICMP_UGT) |
| 2693 | return getFalse(ITy); |
| 2694 | if (Pred == ICmpInst::ICMP_ULE) |
| 2695 | return getTrue(ITy); |
| 2696 | } |
| 2697 | // icmp pred X, (and X, Y) |
| 2698 | if (RBO && match(RBO, m_CombineOr(m_And(m_Value(), m_Specific(LHS)), |
| 2699 | m_And(m_Specific(LHS), m_Value())))) { |
| 2700 | if (Pred == ICmpInst::ICMP_UGE) |
| 2701 | return getTrue(ITy); |
| 2702 | if (Pred == ICmpInst::ICMP_ULT) |
| 2703 | return getFalse(ITy); |
| 2704 | } |
| 2705 | |
| 2706 | // 0 - (zext X) pred C |
| 2707 | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) { |
| 2708 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2709 | if (RHSC->getValue().isStrictlyPositive()) { |
| 2710 | if (Pred == ICmpInst::ICMP_SLT) |
| 2711 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2712 | if (Pred == ICmpInst::ICMP_SGE) |
| 2713 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2714 | if (Pred == ICmpInst::ICMP_EQ) |
| 2715 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2716 | if (Pred == ICmpInst::ICMP_NE) |
| 2717 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2718 | } |
| 2719 | if (RHSC->getValue().isNonNegative()) { |
| 2720 | if (Pred == ICmpInst::ICMP_SLE) |
| 2721 | return ConstantInt::getTrue(RHSC->getContext()); |
| 2722 | if (Pred == ICmpInst::ICMP_SGT) |
| 2723 | return ConstantInt::getFalse(RHSC->getContext()); |
| 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | // icmp pred (urem X, Y), Y |
| 2729 | if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
| 2730 | bool KnownNonNegative, KnownNegative; |
| 2731 | switch (Pred) { |
| 2732 | default: |
| 2733 | break; |
| 2734 | case ICmpInst::ICMP_SGT: |
| 2735 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2736 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2737 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2738 | if (!KnownNonNegative) |
| 2739 | break; |
| 2740 | LLVM_FALLTHROUGH; |
| 2741 | case ICmpInst::ICMP_EQ: |
| 2742 | case ICmpInst::ICMP_UGT: |
| 2743 | case ICmpInst::ICMP_UGE: |
| 2744 | return getFalse(ITy); |
| 2745 | case ICmpInst::ICMP_SLT: |
| 2746 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2747 | ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2748 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2749 | if (!KnownNonNegative) |
| 2750 | break; |
| 2751 | LLVM_FALLTHROUGH; |
| 2752 | case ICmpInst::ICMP_NE: |
| 2753 | case ICmpInst::ICMP_ULT: |
| 2754 | case ICmpInst::ICMP_ULE: |
| 2755 | return getTrue(ITy); |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | // icmp pred X, (urem Y, X) |
| 2760 | if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) { |
| 2761 | bool KnownNonNegative, KnownNegative; |
| 2762 | switch (Pred) { |
| 2763 | default: |
| 2764 | break; |
| 2765 | case ICmpInst::ICMP_SGT: |
| 2766 | case ICmpInst::ICMP_SGE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2767 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2768 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2769 | if (!KnownNonNegative) |
| 2770 | break; |
| 2771 | LLVM_FALLTHROUGH; |
| 2772 | case ICmpInst::ICMP_NE: |
| 2773 | case ICmpInst::ICMP_UGT: |
| 2774 | case ICmpInst::ICMP_UGE: |
| 2775 | return getTrue(ITy); |
| 2776 | case ICmpInst::ICMP_SLT: |
| 2777 | case ICmpInst::ICMP_SLE: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 2778 | ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC, |
| 2779 | Q.CxtI, Q.DT); |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2780 | if (!KnownNonNegative) |
| 2781 | break; |
| 2782 | LLVM_FALLTHROUGH; |
| 2783 | case ICmpInst::ICMP_EQ: |
| 2784 | case ICmpInst::ICMP_ULT: |
| 2785 | case ICmpInst::ICMP_ULE: |
| 2786 | return getFalse(ITy); |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | // x >> y <=u x |
| 2791 | // x udiv y <=u x. |
| 2792 | if (LBO && (match(LBO, m_LShr(m_Specific(RHS), m_Value())) || |
| 2793 | match(LBO, m_UDiv(m_Specific(RHS), m_Value())))) { |
| 2794 | // icmp pred (X op Y), X |
| 2795 | if (Pred == ICmpInst::ICMP_UGT) |
| 2796 | return getFalse(ITy); |
| 2797 | if (Pred == ICmpInst::ICMP_ULE) |
| 2798 | return getTrue(ITy); |
| 2799 | } |
| 2800 | |
| 2801 | // x >=u x >> y |
| 2802 | // x >=u x udiv y. |
| 2803 | if (RBO && (match(RBO, m_LShr(m_Specific(LHS), m_Value())) || |
| 2804 | match(RBO, m_UDiv(m_Specific(LHS), m_Value())))) { |
| 2805 | // icmp pred X, (X op Y) |
| 2806 | if (Pred == ICmpInst::ICMP_ULT) |
| 2807 | return getFalse(ITy); |
| 2808 | if (Pred == ICmpInst::ICMP_UGE) |
| 2809 | return getTrue(ITy); |
| 2810 | } |
| 2811 | |
| 2812 | // handle: |
| 2813 | // CI2 << X == CI |
| 2814 | // CI2 << X != CI |
| 2815 | // |
| 2816 | // where CI2 is a power of 2 and CI isn't |
| 2817 | if (auto *CI = dyn_cast<ConstantInt>(RHS)) { |
| 2818 | const APInt *CI2Val, *CIVal = &CI->getValue(); |
| 2819 | if (LBO && match(LBO, m_Shl(m_APInt(CI2Val), m_Value())) && |
| 2820 | CI2Val->isPowerOf2()) { |
| 2821 | if (!CIVal->isPowerOf2()) { |
| 2822 | // CI2 << X can equal zero in some circumstances, |
| 2823 | // this simplification is unsafe if CI is zero. |
| 2824 | // |
| 2825 | // We know it is safe if: |
| 2826 | // - The shift is nsw, we can't shift out the one bit. |
| 2827 | // - The shift is nuw, we can't shift out the one bit. |
| 2828 | // - CI2 is one |
| 2829 | // - CI isn't zero |
| 2830 | if (LBO->hasNoSignedWrap() || LBO->hasNoUnsignedWrap() || |
| 2831 | *CI2Val == 1 || !CI->isZero()) { |
| 2832 | if (Pred == ICmpInst::ICMP_EQ) |
| 2833 | return ConstantInt::getFalse(RHS->getContext()); |
| 2834 | if (Pred == ICmpInst::ICMP_NE) |
| 2835 | return ConstantInt::getTrue(RHS->getContext()); |
| 2836 | } |
| 2837 | } |
| 2838 | if (CIVal->isSignBit() && *CI2Val == 1) { |
| 2839 | if (Pred == ICmpInst::ICMP_UGT) |
| 2840 | return ConstantInt::getFalse(RHS->getContext()); |
| 2841 | if (Pred == ICmpInst::ICMP_ULE) |
| 2842 | return ConstantInt::getTrue(RHS->getContext()); |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() && |
| 2848 | LBO->getOperand(1) == RBO->getOperand(1)) { |
| 2849 | switch (LBO->getOpcode()) { |
| 2850 | default: |
| 2851 | break; |
| 2852 | case Instruction::UDiv: |
| 2853 | case Instruction::LShr: |
| 2854 | if (ICmpInst::isSigned(Pred)) |
| 2855 | break; |
| 2856 | LLVM_FALLTHROUGH; |
| 2857 | case Instruction::SDiv: |
| 2858 | case Instruction::AShr: |
| 2859 | if (!LBO->isExact() || !RBO->isExact()) |
| 2860 | break; |
| 2861 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2862 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2863 | return V; |
| 2864 | break; |
| 2865 | case Instruction::Shl: { |
| 2866 | bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap(); |
| 2867 | bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap(); |
| 2868 | if (!NUW && !NSW) |
| 2869 | break; |
| 2870 | if (!NSW && ICmpInst::isSigned(Pred)) |
| 2871 | break; |
| 2872 | if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), |
| 2873 | RBO->getOperand(0), Q, MaxRecurse - 1)) |
| 2874 | return V; |
| 2875 | break; |
| 2876 | } |
| 2877 | } |
| 2878 | } |
| 2879 | return nullptr; |
| 2880 | } |
| 2881 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 2882 | /// Simplify integer comparisons where at least one operand of the compare |
| 2883 | /// matches an integer min/max idiom. |
| 2884 | static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, |
| 2885 | Value *RHS, const Query &Q, |
| 2886 | unsigned MaxRecurse) { |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 2887 | Type *ITy = GetCompareTy(LHS); // The return type. |
| 2888 | Value *A, *B; |
| 2889 | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
| 2890 | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
| 2891 | |
| 2892 | // Signed variants on "max(a,b)>=a -> true". |
| 2893 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2894 | if (A != RHS) |
| 2895 | std::swap(A, B); // smax(A, B) pred A. |
| 2896 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2897 | // We analyze this as smax(A, B) pred A. |
| 2898 | P = Pred; |
| 2899 | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
| 2900 | (A == LHS || B == LHS)) { |
| 2901 | if (A != LHS) |
| 2902 | std::swap(A, B); // A pred smax(A, B). |
| 2903 | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
| 2904 | // We analyze this as smax(A, B) swapped-pred A. |
| 2905 | P = CmpInst::getSwappedPredicate(Pred); |
| 2906 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2907 | (A == RHS || B == RHS)) { |
| 2908 | if (A != RHS) |
| 2909 | std::swap(A, B); // smin(A, B) pred A. |
| 2910 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2911 | // We analyze this as smax(-A, -B) swapped-pred -A. |
| 2912 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2913 | P = CmpInst::getSwappedPredicate(Pred); |
| 2914 | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
| 2915 | (A == LHS || B == LHS)) { |
| 2916 | if (A != LHS) |
| 2917 | std::swap(A, B); // A pred smin(A, B). |
| 2918 | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
| 2919 | // We analyze this as smax(-A, -B) pred -A. |
| 2920 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2921 | P = Pred; |
| 2922 | } |
| 2923 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2924 | // Cases correspond to "max(A, B) p A". |
| 2925 | switch (P) { |
| 2926 | default: |
| 2927 | break; |
| 2928 | case CmpInst::ICMP_EQ: |
| 2929 | case CmpInst::ICMP_SLE: |
| 2930 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 2931 | // in the max/min; if so, we can just return that. |
| 2932 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 2933 | return V; |
| 2934 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 2935 | return V; |
| 2936 | // Otherwise, see if "A EqP B" simplifies. |
| 2937 | if (MaxRecurse) |
| 2938 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 2939 | return V; |
| 2940 | break; |
| 2941 | case CmpInst::ICMP_NE: |
| 2942 | case CmpInst::ICMP_SGT: { |
| 2943 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 2944 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 2945 | // tested in the max/min; if so, we can just return that. |
| 2946 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 2947 | return V; |
| 2948 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 2949 | return V; |
| 2950 | // Otherwise, see if "A InvEqP B" simplifies. |
| 2951 | if (MaxRecurse) |
| 2952 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 2953 | return V; |
| 2954 | break; |
| 2955 | } |
| 2956 | case CmpInst::ICMP_SGE: |
| 2957 | // Always true. |
| 2958 | return getTrue(ITy); |
| 2959 | case CmpInst::ICMP_SLT: |
| 2960 | // Always false. |
| 2961 | return getFalse(ITy); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | // Unsigned variants on "max(a,b)>=a -> true". |
| 2966 | P = CmpInst::BAD_ICMP_PREDICATE; |
| 2967 | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) { |
| 2968 | if (A != RHS) |
| 2969 | std::swap(A, B); // umax(A, B) pred A. |
| 2970 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2971 | // We analyze this as umax(A, B) pred A. |
| 2972 | P = Pred; |
| 2973 | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
| 2974 | (A == LHS || B == LHS)) { |
| 2975 | if (A != LHS) |
| 2976 | std::swap(A, B); // A pred umax(A, B). |
| 2977 | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
| 2978 | // We analyze this as umax(A, B) swapped-pred A. |
| 2979 | P = CmpInst::getSwappedPredicate(Pred); |
| 2980 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2981 | (A == RHS || B == RHS)) { |
| 2982 | if (A != RHS) |
| 2983 | std::swap(A, B); // umin(A, B) pred A. |
| 2984 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2985 | // We analyze this as umax(-A, -B) swapped-pred -A. |
| 2986 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2987 | P = CmpInst::getSwappedPredicate(Pred); |
| 2988 | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
| 2989 | (A == LHS || B == LHS)) { |
| 2990 | if (A != LHS) |
| 2991 | std::swap(A, B); // A pred umin(A, B). |
| 2992 | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
| 2993 | // We analyze this as umax(-A, -B) pred -A. |
| 2994 | // Note that we do not need to actually form -A or -B thanks to EqP. |
| 2995 | P = Pred; |
| 2996 | } |
| 2997 | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
| 2998 | // Cases correspond to "max(A, B) p A". |
| 2999 | switch (P) { |
| 3000 | default: |
| 3001 | break; |
| 3002 | case CmpInst::ICMP_EQ: |
| 3003 | case CmpInst::ICMP_ULE: |
| 3004 | // Equivalent to "A EqP B". This may be the same as the condition tested |
| 3005 | // in the max/min; if so, we can just return that. |
| 3006 | if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B)) |
| 3007 | return V; |
| 3008 | if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B)) |
| 3009 | return V; |
| 3010 | // Otherwise, see if "A EqP B" simplifies. |
| 3011 | if (MaxRecurse) |
| 3012 | if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
| 3013 | return V; |
| 3014 | break; |
| 3015 | case CmpInst::ICMP_NE: |
| 3016 | case CmpInst::ICMP_UGT: { |
| 3017 | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
| 3018 | // Equivalent to "A InvEqP B". This may be the same as the condition |
| 3019 | // tested in the max/min; if so, we can just return that. |
| 3020 | if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B)) |
| 3021 | return V; |
| 3022 | if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B)) |
| 3023 | return V; |
| 3024 | // Otherwise, see if "A InvEqP B" simplifies. |
| 3025 | if (MaxRecurse) |
| 3026 | if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
| 3027 | return V; |
| 3028 | break; |
| 3029 | } |
| 3030 | case CmpInst::ICMP_UGE: |
| 3031 | // Always true. |
| 3032 | return getTrue(ITy); |
| 3033 | case CmpInst::ICMP_ULT: |
| 3034 | // Always false. |
| 3035 | return getFalse(ITy); |
| 3036 | } |
| 3037 | } |
| 3038 | |
| 3039 | // Variants on "max(x,y) >= min(x,z)". |
| 3040 | Value *C, *D; |
| 3041 | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
| 3042 | match(RHS, m_SMin(m_Value(C), m_Value(D))) && |
| 3043 | (A == C || A == D || B == C || B == D)) { |
| 3044 | // max(x, ?) pred min(x, ?). |
| 3045 | if (Pred == CmpInst::ICMP_SGE) |
| 3046 | // Always true. |
| 3047 | return getTrue(ITy); |
| 3048 | if (Pred == CmpInst::ICMP_SLT) |
| 3049 | // Always false. |
| 3050 | return getFalse(ITy); |
| 3051 | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
| 3052 | match(RHS, m_SMax(m_Value(C), m_Value(D))) && |
| 3053 | (A == C || A == D || B == C || B == D)) { |
| 3054 | // min(x, ?) pred max(x, ?). |
| 3055 | if (Pred == CmpInst::ICMP_SLE) |
| 3056 | // Always true. |
| 3057 | return getTrue(ITy); |
| 3058 | if (Pred == CmpInst::ICMP_SGT) |
| 3059 | // Always false. |
| 3060 | return getFalse(ITy); |
| 3061 | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
| 3062 | match(RHS, m_UMin(m_Value(C), m_Value(D))) && |
| 3063 | (A == C || A == D || B == C || B == D)) { |
| 3064 | // max(x, ?) pred min(x, ?). |
| 3065 | if (Pred == CmpInst::ICMP_UGE) |
| 3066 | // Always true. |
| 3067 | return getTrue(ITy); |
| 3068 | if (Pred == CmpInst::ICMP_ULT) |
| 3069 | // Always false. |
| 3070 | return getFalse(ITy); |
| 3071 | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
| 3072 | match(RHS, m_UMax(m_Value(C), m_Value(D))) && |
| 3073 | (A == C || A == D || B == C || B == D)) { |
| 3074 | // min(x, ?) pred max(x, ?). |
| 3075 | if (Pred == CmpInst::ICMP_ULE) |
| 3076 | // Always true. |
| 3077 | return getTrue(ITy); |
| 3078 | if (Pred == CmpInst::ICMP_UGT) |
| 3079 | // Always false. |
| 3080 | return getFalse(ITy); |
| 3081 | } |
| 3082 | |
| 3083 | return nullptr; |
| 3084 | } |
| 3085 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3086 | /// Given operands for an ICmpInst, see if we can fold the result. |
| 3087 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3088 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3089 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3090 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3091 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3092 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3093 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | cdfb80d | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3094 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3095 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3096 | |
| 3097 | // If we have a constant, make sure it is on the RHS. |
| 3098 | std::swap(LHS, RHS); |
| 3099 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3100 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3101 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3102 | Type *ITy = GetCompareTy(LHS); // The return type. |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3103 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3104 | // icmp X, X -> true/false |
Chris Lattner | 3afc072 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 3105 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 3106 | // because X could be 0. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3107 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3108 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3109 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3110 | if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) |
| 3111 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3112 | |
Sanjay Patel | dc65a27 | 2016-12-03 17:30:22 +0000 | [diff] [blame] | 3113 | if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) |
| 3114 | return V; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 3115 | |
Sanjay Patel | 67bde28 | 2016-08-22 23:12:02 +0000 | [diff] [blame] | 3116 | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS)) |
| 3117 | return V; |
Duncan Sands | 8d25a7c | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 3118 | |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3119 | // If both operands have range metadata, use the metadata |
| 3120 | // to simplify the comparison. |
| 3121 | if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) { |
| 3122 | auto RHS_Instr = dyn_cast<Instruction>(RHS); |
| 3123 | auto LHS_Instr = dyn_cast<Instruction>(LHS); |
| 3124 | |
| 3125 | if (RHS_Instr->getMetadata(LLVMContext::MD_range) && |
| 3126 | LHS_Instr->getMetadata(LLVMContext::MD_range)) { |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 3127 | auto RHS_CR = getConstantRangeFromMetadata( |
| 3128 | *RHS_Instr->getMetadata(LLVMContext::MD_range)); |
| 3129 | auto LHS_CR = getConstantRangeFromMetadata( |
| 3130 | *LHS_Instr->getMetadata(LLVMContext::MD_range)); |
Chen Li | 7452d95 | 2015-09-26 03:26:47 +0000 | [diff] [blame] | 3131 | |
| 3132 | auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR); |
| 3133 | if (Satisfied_CR.contains(LHS_CR)) |
| 3134 | return ConstantInt::getTrue(RHS->getContext()); |
| 3135 | |
| 3136 | auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion( |
| 3137 | CmpInst::getInversePredicate(Pred), RHS_CR); |
| 3138 | if (InversedSatisfied_CR.contains(LHS_CR)) |
| 3139 | return ConstantInt::getFalse(RHS->getContext()); |
| 3140 | } |
| 3141 | } |
| 3142 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3143 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 3144 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 3145 | Instruction *LI = cast<CastInst>(LHS); |
| 3146 | Value *SrcOp = LI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3147 | Type *SrcTy = SrcOp->getType(); |
| 3148 | Type *DstTy = LI->getType(); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3149 | |
| 3150 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 3151 | // if the integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3152 | if (MaxRecurse && isa<PtrToIntInst>(LI) && |
| 3153 | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) { |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3154 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 3155 | // Transfer the cast to the constant. |
| 3156 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 3157 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3158 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3159 | return V; |
| 3160 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 3161 | if (RI->getOperand(0)->getType() == SrcTy) |
| 3162 | // Compare without the cast. |
| 3163 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3164 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3165 | return V; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | if (isa<ZExtInst>(LHS)) { |
| 3170 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 3171 | // same type. |
| 3172 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 3173 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3174 | // Compare X and Y. Note that signed predicates become unsigned. |
| 3175 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3176 | SrcOp, RI->getOperand(0), Q, |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3177 | MaxRecurse-1)) |
| 3178 | return V; |
| 3179 | } |
| 3180 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 3181 | // too. If not, then try to deduce the result of the comparison. |
| 3182 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3183 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3184 | // reextended to DstTy. |
| 3185 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3186 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 3187 | |
| 3188 | // If the re-extended constant didn't change then this is effectively |
| 3189 | // also a case of comparing two zero-extended values. |
| 3190 | if (RExt == CI && MaxRecurse) |
| 3191 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3192 | SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3193 | return V; |
| 3194 | |
| 3195 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 3196 | // there. Use this to work out the result of the comparison. |
| 3197 | if (RExt != CI) { |
| 3198 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3199 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3200 | // LHS <u RHS. |
| 3201 | case ICmpInst::ICMP_EQ: |
| 3202 | case ICmpInst::ICMP_UGT: |
| 3203 | case ICmpInst::ICMP_UGE: |
| 3204 | return ConstantInt::getFalse(CI->getContext()); |
| 3205 | |
| 3206 | case ICmpInst::ICMP_NE: |
| 3207 | case ICmpInst::ICMP_ULT: |
| 3208 | case ICmpInst::ICMP_ULE: |
| 3209 | return ConstantInt::getTrue(CI->getContext()); |
| 3210 | |
| 3211 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 3212 | // is non-negative then LHS <s RHS. |
| 3213 | case ICmpInst::ICMP_SGT: |
| 3214 | case ICmpInst::ICMP_SGE: |
| 3215 | return CI->getValue().isNegative() ? |
| 3216 | ConstantInt::getTrue(CI->getContext()) : |
| 3217 | ConstantInt::getFalse(CI->getContext()); |
| 3218 | |
| 3219 | case ICmpInst::ICMP_SLT: |
| 3220 | case ICmpInst::ICMP_SLE: |
| 3221 | return CI->getValue().isNegative() ? |
| 3222 | ConstantInt::getFalse(CI->getContext()) : |
| 3223 | ConstantInt::getTrue(CI->getContext()); |
| 3224 | } |
| 3225 | } |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | if (isa<SExtInst>(LHS)) { |
| 3230 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 3231 | // same type. |
| 3232 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 3233 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 3234 | // Compare X and Y. Note that the predicate does not change. |
| 3235 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3236 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3237 | return V; |
| 3238 | } |
| 3239 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 3240 | // too. If not, then try to deduce the result of the comparison. |
| 3241 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 3242 | // Compute the constant that would happen if we truncated to SrcTy then |
| 3243 | // reextended to DstTy. |
| 3244 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 3245 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 3246 | |
| 3247 | // If the re-extended constant didn't change then this is effectively |
| 3248 | // also a case of comparing two sign-extended values. |
| 3249 | if (RExt == CI && MaxRecurse) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3250 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3251 | return V; |
| 3252 | |
| 3253 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 3254 | // bits there. Use this to work out the result of the comparison. |
| 3255 | if (RExt != CI) { |
| 3256 | switch (Pred) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3257 | default: llvm_unreachable("Unknown ICmp predicate!"); |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3258 | case ICmpInst::ICMP_EQ: |
| 3259 | return ConstantInt::getFalse(CI->getContext()); |
| 3260 | case ICmpInst::ICMP_NE: |
| 3261 | return ConstantInt::getTrue(CI->getContext()); |
| 3262 | |
| 3263 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 3264 | // LHS >s RHS. |
| 3265 | case ICmpInst::ICMP_SGT: |
| 3266 | case ICmpInst::ICMP_SGE: |
| 3267 | return CI->getValue().isNegative() ? |
| 3268 | ConstantInt::getTrue(CI->getContext()) : |
| 3269 | ConstantInt::getFalse(CI->getContext()); |
| 3270 | case ICmpInst::ICMP_SLT: |
| 3271 | case ICmpInst::ICMP_SLE: |
| 3272 | return CI->getValue().isNegative() ? |
| 3273 | ConstantInt::getFalse(CI->getContext()) : |
| 3274 | ConstantInt::getTrue(CI->getContext()); |
| 3275 | |
| 3276 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 3277 | // LHS >u RHS. |
| 3278 | case ICmpInst::ICMP_UGT: |
| 3279 | case ICmpInst::ICMP_UGE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3280 | // Comparison is true iff the LHS <s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3281 | if (MaxRecurse) |
| 3282 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 3283 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3284 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3285 | return V; |
| 3286 | break; |
| 3287 | case ICmpInst::ICMP_ULT: |
| 3288 | case ICmpInst::ICMP_ULE: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3289 | // Comparison is true iff the LHS >=s 0. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3290 | if (MaxRecurse) |
| 3291 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 3292 | Constant::getNullValue(SrcTy), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3293 | Q, MaxRecurse-1)) |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3294 | return V; |
| 3295 | break; |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3302 | // icmp eq|ne X, Y -> false|true if X != Y |
| 3303 | if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3304 | isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) { |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 3305 | LLVMContext &Ctx = LHS->getType()->getContext(); |
| 3306 | return Pred == ICmpInst::ICMP_NE ? |
| 3307 | ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx); |
| 3308 | } |
Junmo Park | 53470fc | 2016-04-05 21:14:31 +0000 | [diff] [blame] | 3309 | |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3310 | if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse)) |
| 3311 | return V; |
Duncan Sands | d114ab3 | 2011-02-13 17:15:40 +0000 | [diff] [blame] | 3312 | |
Sanjay Patel | 35289c6 | 2016-12-10 17:40:47 +0000 | [diff] [blame] | 3313 | if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) |
Sanjay Patel | 9d5b5e3 | 2016-12-03 18:03:53 +0000 | [diff] [blame] | 3314 | return V; |
Duncan Sands | a228785 | 2011-05-04 16:05:05 +0000 | [diff] [blame] | 3315 | |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3316 | // Simplify comparisons of related pointers using a powerful, recursive |
| 3317 | // GEP-walk when we have target data available.. |
Dan Gohman | 18c77a1 | 2013-01-31 02:50:36 +0000 | [diff] [blame] | 3318 | if (LHS->getType()->isPointerTy()) |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 3319 | 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] | 3320 | return C; |
David Majnemer | dc8767a | 2016-08-07 07:58:10 +0000 | [diff] [blame] | 3321 | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
| 3322 | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
| 3323 | if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
| 3324 | Q.DL.getTypeSizeInBits(CLHS->getType()) && |
| 3325 | Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) == |
| 3326 | Q.DL.getTypeSizeInBits(CRHS->getType())) |
| 3327 | if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, |
| 3328 | CLHS->getPointerOperand(), |
| 3329 | CRHS->getPointerOperand())) |
| 3330 | return C; |
Chandler Carruth | 8059c84 | 2012-03-25 21:28:14 +0000 | [diff] [blame] | 3331 | |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3332 | if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { |
| 3333 | if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { |
| 3334 | if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && |
| 3335 | GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && |
| 3336 | (ICmpInst::isEquality(Pred) || |
| 3337 | (GLHS->isInBounds() && GRHS->isInBounds() && |
| 3338 | Pred == ICmpInst::getSignedPredicate(Pred)))) { |
| 3339 | // The bases are equal and the indices are constant. Build a constant |
| 3340 | // expression GEP with the same indices and a null base pointer to see |
| 3341 | // what constant folding can make out of it. |
| 3342 | Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); |
| 3343 | SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3344 | Constant *NewLHS = ConstantExpr::getGetElementPtr( |
| 3345 | GLHS->getSourceElementType(), Null, IndicesLHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3346 | |
| 3347 | SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3348 | Constant *NewRHS = ConstantExpr::getGetElementPtr( |
| 3349 | GLHS->getSourceElementType(), Null, IndicesRHS); |
Nick Lewycky | 3db143e | 2012-02-26 02:09:49 +0000 | [diff] [blame] | 3350 | return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3355 | // If a bit is known to be zero for A and known to be one for B, |
| 3356 | // then A and B cannot be equal. |
| 3357 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3358 | const APInt *RHSVal; |
| 3359 | if (match(RHS, m_APInt(RHSVal))) { |
| 3360 | unsigned BitWidth = RHSVal->getBitWidth(); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3361 | APInt LHSKnownZero(BitWidth, 0); |
| 3362 | APInt LHSKnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3363 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC, |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3364 | Q.CxtI, Q.DT); |
Sanjay Patel | bcaf6f3 | 2016-08-04 17:48:04 +0000 | [diff] [blame] | 3365 | if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0)) |
| 3366 | return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy) |
| 3367 | : ConstantInt::getTrue(ITy); |
David Majnemer | 5854e9f | 2014-11-16 02:20:08 +0000 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3371 | // If the comparison is with the result of a select instruction, check whether |
| 3372 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3373 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3374 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3375 | return V; |
| 3376 | |
| 3377 | // If the comparison is with the result of a phi instruction, check whether |
| 3378 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3379 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3380 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3381 | return V; |
Duncan Sands | f532d31 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 3382 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3383 | return nullptr; |
Chris Lattner | 084a1b5 | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3386 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3387 | const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3388 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3389 | const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 85dbea9 | 2015-12-24 09:08:08 +0000 | [diff] [blame] | 3390 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3391 | return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3392 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3395 | /// Given operands for an FCmpInst, see if we can fold the result. |
| 3396 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3397 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3398 | FastMathFlags FMF, const Query &Q, |
| 3399 | unsigned MaxRecurse) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3400 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 3401 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 3402 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3403 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3404 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 3405 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 3407 | // If we have a constant, make sure it is on the RHS. |
| 3408 | std::swap(LHS, RHS); |
| 3409 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3410 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3411 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3412 | // Fold trivial predicates. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3413 | Type *RetTy = GetCompareTy(LHS); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3414 | if (Pred == FCmpInst::FCMP_FALSE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3415 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3416 | if (Pred == FCmpInst::FCMP_TRUE) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3417 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3418 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3419 | // UNO/ORD predicates can be trivially folded if NaNs are ignored. |
| 3420 | if (FMF.noNaNs()) { |
| 3421 | if (Pred == FCmpInst::FCMP_UNO) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3422 | return getFalse(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3423 | if (Pred == FCmpInst::FCMP_ORD) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3424 | return getTrue(RetTy); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3427 | // fcmp pred x, undef and fcmp pred undef, x |
| 3428 | // fold to true if unordered, false if ordered |
| 3429 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) { |
| 3430 | // Choosing NaN for the undef will always make unordered comparison succeed |
| 3431 | // and ordered comparison fail. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3432 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
Mehdi Amini | eb242a5 | 2015-03-09 03:20:25 +0000 | [diff] [blame] | 3433 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3434 | |
| 3435 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3436 | if (LHS == RHS) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3437 | if (CmpInst::isTrueWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3438 | return getTrue(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3439 | if (CmpInst::isFalseWhenEqual(Pred)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3440 | return getFalse(RetTy); |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3441 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3443 | // Handle fcmp with constant RHS |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3444 | const ConstantFP *CFP = nullptr; |
| 3445 | if (const auto *RHSC = dyn_cast<Constant>(RHS)) { |
| 3446 | if (RHS->getType()->isVectorTy()) |
| 3447 | CFP = dyn_cast_or_null<ConstantFP>(RHSC->getSplatValue()); |
| 3448 | else |
| 3449 | CFP = dyn_cast<ConstantFP>(RHSC); |
| 3450 | } |
| 3451 | if (CFP) { |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3452 | // 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] | 3453 | if (CFP->getValueAPF().isNaN()) { |
| 3454 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
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 | assert(FCmpInst::isUnordered(Pred) && |
| 3457 | "Comparison must be either ordered or unordered!"); |
| 3458 | // True if unordered. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3459 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3460 | } |
| 3461 | // Check whether the constant is an infinity. |
| 3462 | if (CFP->getValueAPF().isInfinity()) { |
| 3463 | if (CFP->getValueAPF().isNegative()) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3464 | switch (Pred) { |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3465 | case FCmpInst::FCMP_OLT: |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3466 | // No value is ordered and less than negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3467 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3468 | case FCmpInst::FCMP_UGE: |
| 3469 | // All values are unordered with or at least negative infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3470 | return getTrue(RetTy); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3471 | default: |
| 3472 | break; |
| 3473 | } |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3474 | } else { |
| 3475 | switch (Pred) { |
| 3476 | case FCmpInst::FCMP_OGT: |
| 3477 | // No value is ordered and greater than infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3478 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3479 | case FCmpInst::FCMP_ULE: |
| 3480 | // All values are unordered with and at most infinity. |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3481 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3482 | default: |
| 3483 | break; |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | if (CFP->getValueAPF().isZero()) { |
| 3488 | switch (Pred) { |
| 3489 | case FCmpInst::FCMP_UGE: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3490 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3491 | return getTrue(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3492 | break; |
| 3493 | case FCmpInst::FCMP_OLT: |
| 3494 | // X < 0 |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3495 | if (CannotBeOrderedLessThanZero(LHS, Q.TLI)) |
Andrea Di Biagio | bff3fd6 | 2016-09-02 15:55:25 +0000 | [diff] [blame] | 3496 | return getFalse(RetTy); |
Mehdi Amini | 383d7ae | 2015-02-13 07:38:04 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | default: |
| 3499 | break; |
| 3500 | } |
Chris Lattner | ccfdceb | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 3501 | } |
| 3502 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3503 | |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3504 | // If the comparison is with the result of a select instruction, check whether |
| 3505 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3506 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3507 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3508 | return V; |
| 3509 | |
| 3510 | // If the comparison is with the result of a phi instruction, check whether |
| 3511 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 3512 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3513 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | fc5ad3f0 | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 3514 | return V; |
Duncan Sands | a620bd1 | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 3515 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3516 | return nullptr; |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3519 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3520 | FastMathFlags FMF, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 3521 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3522 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3523 | const Instruction *CxtI) { |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 3524 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3525 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3528 | /// See if V simplifies when its operand Op is replaced with RepOp. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3529 | static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
| 3530 | const Query &Q, |
| 3531 | unsigned MaxRecurse) { |
| 3532 | // Trivial replacement. |
| 3533 | if (V == Op) |
| 3534 | return RepOp; |
| 3535 | |
| 3536 | auto *I = dyn_cast<Instruction>(V); |
| 3537 | if (!I) |
| 3538 | return nullptr; |
| 3539 | |
| 3540 | // If this is a binary operator, try to simplify it with the replaced op. |
| 3541 | if (auto *B = dyn_cast<BinaryOperator>(I)) { |
| 3542 | // Consider: |
| 3543 | // %cmp = icmp eq i32 %x, 2147483647 |
| 3544 | // %add = add nsw i32 %x, 1 |
| 3545 | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
| 3546 | // |
| 3547 | // We can't replace %sel with %add unless we strip away the flags. |
| 3548 | if (isa<OverflowingBinaryOperator>(B)) |
| 3549 | if (B->hasNoSignedWrap() || B->hasNoUnsignedWrap()) |
| 3550 | return nullptr; |
| 3551 | if (isa<PossiblyExactOperator>(B)) |
| 3552 | if (B->isExact()) |
| 3553 | return nullptr; |
| 3554 | |
| 3555 | if (MaxRecurse) { |
| 3556 | if (B->getOperand(0) == Op) |
| 3557 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q, |
| 3558 | MaxRecurse - 1); |
| 3559 | if (B->getOperand(1) == Op) |
| 3560 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q, |
| 3561 | MaxRecurse - 1); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | // Same for CmpInsts. |
| 3566 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 3567 | if (MaxRecurse) { |
| 3568 | if (C->getOperand(0) == Op) |
| 3569 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q, |
| 3570 | MaxRecurse - 1); |
| 3571 | if (C->getOperand(1) == Op) |
| 3572 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q, |
| 3573 | MaxRecurse - 1); |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | // TODO: We could hand off more cases to instsimplify here. |
| 3578 | |
| 3579 | // If all operands are constant after substituting Op for RepOp then we can |
| 3580 | // constant fold the instruction. |
| 3581 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 3582 | // Build a list of all constant operands. |
| 3583 | SmallVector<Constant *, 8> ConstOps; |
| 3584 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3585 | if (I->getOperand(i) == Op) |
| 3586 | ConstOps.push_back(CRepOp); |
| 3587 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 3588 | ConstOps.push_back(COp); |
| 3589 | else |
| 3590 | break; |
| 3591 | } |
| 3592 | |
| 3593 | // All operands were constants, fold it. |
| 3594 | if (ConstOps.size() == I->getNumOperands()) { |
| 3595 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 3596 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 3597 | ConstOps[1], Q.DL, Q.TLI); |
| 3598 | |
| 3599 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 3600 | if (!LI->isVolatile()) |
Eduard Burtescu | 1423921 | 2016-01-22 01:17:26 +0000 | [diff] [blame] | 3601 | return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3602 | |
Manuel Jacob | e902459 | 2016-01-21 06:33:22 +0000 | [diff] [blame] | 3603 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | return nullptr; |
| 3608 | } |
| 3609 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3610 | /// Try to simplify a select instruction when its condition operand is an |
| 3611 | /// integer comparison where one operand of the compare is a constant. |
| 3612 | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
| 3613 | const APInt *Y, bool TrueWhenUnset) { |
| 3614 | const APInt *C; |
| 3615 | |
| 3616 | // (X & Y) == 0 ? X & ~Y : X --> X |
| 3617 | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
| 3618 | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3619 | *Y == ~*C) |
| 3620 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3621 | |
| 3622 | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
| 3623 | // (X & Y) != 0 ? X : X & ~Y --> X |
| 3624 | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) && |
| 3625 | *Y == ~*C) |
| 3626 | return TrueWhenUnset ? FalseVal : TrueVal; |
| 3627 | |
| 3628 | if (Y->isPowerOf2()) { |
| 3629 | // (X & Y) == 0 ? X | Y : X --> X | Y |
| 3630 | // (X & Y) != 0 ? X | Y : X --> X |
| 3631 | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3632 | *Y == *C) |
| 3633 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3634 | |
| 3635 | // (X & Y) == 0 ? X : X | Y --> X |
| 3636 | // (X & Y) != 0 ? X : X | Y --> X | Y |
| 3637 | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) && |
| 3638 | *Y == *C) |
| 3639 | return TrueWhenUnset ? TrueVal : FalseVal; |
| 3640 | } |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3641 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3642 | return nullptr; |
| 3643 | } |
| 3644 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3645 | /// An alternative way to test if a bit is set or not uses sgt/slt instead of |
| 3646 | /// eq/ne. |
| 3647 | static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *TrueVal, |
| 3648 | Value *FalseVal, |
| 3649 | bool TrueWhenUnset) { |
| 3650 | unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); |
Sanjay Patel | e9fc79b | 2016-07-21 21:56:00 +0000 | [diff] [blame] | 3651 | if (!BitWidth) |
| 3652 | return nullptr; |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 3653 | |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3654 | APInt MinSignedValue; |
| 3655 | Value *X; |
| 3656 | if (match(CmpLHS, m_Trunc(m_Value(X))) && (X == TrueVal || X == FalseVal)) { |
| 3657 | // icmp slt (trunc X), 0 <--> icmp ne (and X, C), 0 |
| 3658 | // icmp sgt (trunc X), -1 <--> icmp eq (and X, C), 0 |
| 3659 | unsigned DestSize = CmpLHS->getType()->getScalarSizeInBits(); |
| 3660 | MinSignedValue = APInt::getSignedMinValue(DestSize).zext(BitWidth); |
| 3661 | } else { |
| 3662 | // icmp slt X, 0 <--> icmp ne (and X, C), 0 |
| 3663 | // icmp sgt X, -1 <--> icmp eq (and X, C), 0 |
| 3664 | X = CmpLHS; |
| 3665 | MinSignedValue = APInt::getSignedMinValue(BitWidth); |
| 3666 | } |
| 3667 | |
| 3668 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, &MinSignedValue, |
| 3669 | TrueWhenUnset)) |
| 3670 | return V; |
| 3671 | |
| 3672 | return nullptr; |
| 3673 | } |
| 3674 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3675 | /// Try to simplify a select instruction when its condition operand is an |
| 3676 | /// integer comparison. |
| 3677 | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
| 3678 | Value *FalseVal, const Query &Q, |
| 3679 | unsigned MaxRecurse) { |
| 3680 | ICmpInst::Predicate Pred; |
| 3681 | Value *CmpLHS, *CmpRHS; |
| 3682 | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
| 3683 | return nullptr; |
| 3684 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 3685 | // FIXME: This code is nearly duplicated in InstCombine. Using/refactoring |
| 3686 | // decomposeBitTestICmp() might help. |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3687 | if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero())) { |
| 3688 | Value *X; |
| 3689 | const APInt *Y; |
| 3690 | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
| 3691 | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
| 3692 | Pred == ICmpInst::ICMP_EQ)) |
| 3693 | return V; |
| 3694 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3695 | // Comparing signed-less-than 0 checks if the sign bit is set. |
| 3696 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3697 | false)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3698 | return V; |
| 3699 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
Sanjay Patel | a3bfb4e | 2016-07-21 21:26:45 +0000 | [diff] [blame] | 3700 | // Comparing signed-greater-than -1 checks if the sign bit is not set. |
| 3701 | if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, TrueVal, FalseVal, |
| 3702 | true)) |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3703 | return V; |
| 3704 | } |
| 3705 | |
| 3706 | if (CondVal->hasOneUse()) { |
| 3707 | const APInt *C; |
| 3708 | if (match(CmpRHS, m_APInt(C))) { |
| 3709 | // X < MIN ? T : F --> F |
| 3710 | if (Pred == ICmpInst::ICMP_SLT && C->isMinSignedValue()) |
| 3711 | return FalseVal; |
| 3712 | // X < MIN ? T : F --> F |
| 3713 | if (Pred == ICmpInst::ICMP_ULT && C->isMinValue()) |
| 3714 | return FalseVal; |
| 3715 | // X > MAX ? T : F --> F |
| 3716 | if (Pred == ICmpInst::ICMP_SGT && C->isMaxSignedValue()) |
| 3717 | return FalseVal; |
| 3718 | // X > MAX ? T : F --> F |
| 3719 | if (Pred == ICmpInst::ICMP_UGT && C->isMaxValue()) |
| 3720 | return FalseVal; |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | // If we have an equality comparison, then we know the value in one of the |
| 3725 | // arms of the select. See if substituting this value into the arm and |
| 3726 | // simplifying the result yields the same value as the other arm. |
| 3727 | if (Pred == ICmpInst::ICMP_EQ) { |
| 3728 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3729 | TrueVal || |
| 3730 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3731 | TrueVal) |
| 3732 | return FalseVal; |
| 3733 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3734 | FalseVal || |
| 3735 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3736 | FalseVal) |
| 3737 | return FalseVal; |
| 3738 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 3739 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3740 | FalseVal || |
| 3741 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3742 | FalseVal) |
| 3743 | return TrueVal; |
| 3744 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) == |
| 3745 | TrueVal || |
| 3746 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) == |
| 3747 | TrueVal) |
| 3748 | return TrueVal; |
| 3749 | } |
| 3750 | |
| 3751 | return nullptr; |
| 3752 | } |
| 3753 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3754 | /// Given operands for a SelectInst, see if we can fold the result. |
| 3755 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3756 | static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, |
| 3757 | Value *FalseVal, const Query &Q, |
| 3758 | unsigned MaxRecurse) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3759 | // select true, X, Y -> X |
| 3760 | // select false, X, Y -> Y |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3761 | if (Constant *CB = dyn_cast<Constant>(CondVal)) { |
| 3762 | if (CB->isAllOnesValue()) |
| 3763 | return TrueVal; |
| 3764 | if (CB->isNullValue()) |
| 3765 | return FalseVal; |
| 3766 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3767 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3768 | // select C, X, X -> X |
Duncan Sands | 772749a | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 3769 | if (TrueVal == FalseVal) |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3770 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3771 | |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3772 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 3773 | if (isa<Constant>(TrueVal)) |
| 3774 | return TrueVal; |
| 3775 | return FalseVal; |
| 3776 | } |
Dan Gohman | 54664ed | 2011-07-01 01:03:43 +0000 | [diff] [blame] | 3777 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 3778 | return FalseVal; |
| 3779 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 3780 | return TrueVal; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3781 | |
Sanjay Patel | 5f5eb58 | 2016-07-18 20:56:53 +0000 | [diff] [blame] | 3782 | if (Value *V = |
| 3783 | simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse)) |
| 3784 | return V; |
David Majnemer | c6a5e1d | 2014-11-27 06:32:46 +0000 | [diff] [blame] | 3785 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3786 | return nullptr; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3789 | Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3790 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3791 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3792 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3793 | const Instruction *CxtI) { |
| 3794 | return ::SimplifySelectInst(Cond, TrueVal, FalseVal, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3795 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3798 | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
| 3799 | /// If not, this returns null. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3800 | static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3801 | const Query &Q, unsigned) { |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3802 | // The type of the GEP pointer operand. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3803 | unsigned AS = |
| 3804 | cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace(); |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3805 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3806 | // getelementptr P -> P. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3807 | if (Ops.size() == 1) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3808 | return Ops[0]; |
| 3809 | |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3810 | // Compute the (pointer) type returned by the GEP instruction. |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3811 | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1)); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3812 | Type *GEPTy = PointerType::get(LastType, AS); |
| 3813 | if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType())) |
| 3814 | GEPTy = VectorType::get(GEPTy, VT->getNumElements()); |
| 3815 | |
| 3816 | if (isa<UndefValue>(Ops[0])) |
Duncan Sands | 8a0f486 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 3817 | return UndefValue::get(GEPTy); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3818 | |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3819 | if (Ops.size() == 2) { |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3820 | // getelementptr P, 0 -> P. |
Benjamin Kramer | 5e1794e | 2014-01-24 17:09:53 +0000 | [diff] [blame] | 3821 | if (match(Ops[1], m_Zero())) |
| 3822 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3823 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3824 | Type *Ty = SrcTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3825 | if (Ty->isSized()) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3826 | Value *P; |
| 3827 | uint64_t C; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3828 | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3829 | // getelementptr P, N -> P if P points to a type of zero size. |
| 3830 | if (TyAllocSize == 0) |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3831 | return Ops[0]; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3832 | |
| 3833 | // The following transforms are only safe if the ptrtoint cast |
| 3834 | // doesn't truncate the pointers. |
| 3835 | if (Ops[1]->getType()->getScalarSizeInBits() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3836 | Q.DL.getPointerSizeInBits(AS)) { |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3837 | auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { |
| 3838 | if (match(P, m_Zero())) |
| 3839 | return Constant::getNullValue(GEPTy); |
| 3840 | Value *Temp; |
| 3841 | if (match(P, m_PtrToInt(m_Value(Temp)))) |
David Majnemer | 11ca297 | 2014-08-27 20:08:34 +0000 | [diff] [blame] | 3842 | if (Temp->getType() == GEPTy) |
| 3843 | return Temp; |
Nico Weber | 48c8240 | 2014-08-27 20:06:19 +0000 | [diff] [blame] | 3844 | return nullptr; |
| 3845 | }; |
| 3846 | |
| 3847 | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
| 3848 | if (TyAllocSize == 1 && |
| 3849 | match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) |
| 3850 | if (Value *R = PtrToIntOrZero(P)) |
| 3851 | return R; |
| 3852 | |
| 3853 | // getelementptr V, (ashr (sub P, V), C) -> Q |
| 3854 | // if P points to a type of size 1 << C. |
| 3855 | if (match(Ops[1], |
| 3856 | m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3857 | m_ConstantInt(C))) && |
| 3858 | TyAllocSize == 1ULL << C) |
| 3859 | if (Value *R = PtrToIntOrZero(P)) |
| 3860 | return R; |
| 3861 | |
| 3862 | // getelementptr V, (sdiv (sub P, V), C) -> Q |
| 3863 | // if P points to a type of size C. |
| 3864 | if (match(Ops[1], |
| 3865 | m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), |
| 3866 | m_SpecificInt(TyAllocSize)))) |
| 3867 | if (Value *R = PtrToIntOrZero(P)) |
| 3868 | return R; |
| 3869 | } |
Duncan Sands | cf4bceb | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 3870 | } |
| 3871 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3872 | |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3873 | if (Q.DL.getTypeAllocSize(LastType) == 1 && |
| 3874 | all_of(Ops.slice(1).drop_back(1), |
| 3875 | [](Value *Idx) { return match(Idx, m_Zero()); })) { |
| 3876 | unsigned PtrWidth = |
| 3877 | Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); |
| 3878 | if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) { |
| 3879 | APInt BasePtrOffset(PtrWidth, 0); |
| 3880 | Value *StrippedBasePtr = |
| 3881 | Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, |
| 3882 | BasePtrOffset); |
| 3883 | |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3884 | // gep (gep V, C), (sub 0, V) -> C |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3885 | if (match(Ops.back(), |
| 3886 | m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { |
| 3887 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
| 3888 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3889 | } |
David Majnemer | 5c5df62 | 2016-08-16 06:13:46 +0000 | [diff] [blame] | 3890 | // gep (gep V, C), (xor V, -1) -> C-1 |
| 3891 | if (match(Ops.back(), |
| 3892 | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) { |
| 3893 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
| 3894 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
| 3895 | } |
David Majnemer | d150137 | 2016-08-07 07:58:12 +0000 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3899 | // Check to see if this is constant foldable. |
Jay Foad | b992a63 | 2011-07-19 15:07:52 +0000 | [diff] [blame] | 3900 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3901 | if (!isa<Constant>(Ops[i])) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3902 | return nullptr; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 3903 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 3904 | return ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]), |
| 3905 | Ops.slice(1)); |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3908 | Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, |
| 3909 | const DataLayout &DL, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3910 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3911 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 3912 | const Instruction *CxtI) { |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 3913 | return ::SimplifyGEPInst(SrcTy, Ops, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3914 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3917 | /// Given operands for an InsertValueInst, see if we can fold the result. |
| 3918 | /// If not, this returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3919 | static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, |
| 3920 | ArrayRef<unsigned> Idxs, const Query &Q, |
| 3921 | unsigned) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3922 | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
| 3923 | if (Constant *CVal = dyn_cast<Constant>(Val)) |
| 3924 | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
| 3925 | |
| 3926 | // insertvalue x, undef, n -> x |
| 3927 | if (match(Val, m_Undef())) |
| 3928 | return Agg; |
| 3929 | |
| 3930 | // insertvalue x, (extractvalue y, n), n |
| 3931 | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
Benjamin Kramer | 4b79c21 | 2011-09-05 18:16:19 +0000 | [diff] [blame] | 3932 | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
| 3933 | EV->getIndices() == Idxs) { |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3934 | // insertvalue undef, (extractvalue y, n), n -> y |
| 3935 | if (match(Agg, m_Undef())) |
| 3936 | return EV->getAggregateOperand(); |
| 3937 | |
| 3938 | // insertvalue y, (extractvalue y, n), n -> y |
| 3939 | if (Agg == EV->getAggregateOperand()) |
| 3940 | return Agg; |
| 3941 | } |
| 3942 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3943 | return nullptr; |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3946 | Value *llvm::SimplifyInsertValueInst( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3947 | Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3948 | const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3949 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3950 | return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 3951 | RecursionLimit); |
| 3952 | } |
| 3953 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3954 | /// Given operands for an ExtractValueInst, see if we can fold the result. |
| 3955 | /// If not, this returns null. |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3956 | static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3957 | const Query &, unsigned) { |
| 3958 | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
| 3959 | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
| 3960 | |
| 3961 | // extractvalue x, (insertvalue y, elt, n), n -> elt |
| 3962 | unsigned NumIdxs = Idxs.size(); |
| 3963 | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
| 3964 | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) { |
| 3965 | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
| 3966 | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
| 3967 | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
| 3968 | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
| 3969 | Idxs.slice(0, NumCommonIdxs)) { |
| 3970 | if (NumIdxs == NumInsertValueIdxs) |
| 3971 | return IVI->getInsertedValueOperand(); |
| 3972 | break; |
| 3973 | } |
| 3974 | } |
| 3975 | |
| 3976 | return nullptr; |
| 3977 | } |
| 3978 | |
| 3979 | Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
| 3980 | const DataLayout &DL, |
| 3981 | const TargetLibraryInfo *TLI, |
| 3982 | const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3983 | AssumptionCache *AC, |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3984 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3985 | return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 3986 | RecursionLimit); |
| 3987 | } |
| 3988 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 3989 | /// Given operands for an ExtractElementInst, see if we can fold the result. |
| 3990 | /// If not, this returns null. |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 3991 | static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const Query &, |
| 3992 | unsigned) { |
| 3993 | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
| 3994 | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
| 3995 | return ConstantFoldExtractElementInstruction(CVec, CIdx); |
| 3996 | |
| 3997 | // The index is not relevant if our vector is a splat. |
| 3998 | if (auto *Splat = CVec->getSplatValue()) |
| 3999 | return Splat; |
| 4000 | |
| 4001 | if (isa<UndefValue>(Vec)) |
| 4002 | return UndefValue::get(Vec->getType()->getVectorElementType()); |
| 4003 | } |
| 4004 | |
| 4005 | // If extracting a specified index from the vector, see if we can recursively |
| 4006 | // find a previously computed scalar that was inserted into the vector. |
David Majnemer | 8e335ca | 2015-08-18 22:18:22 +0000 | [diff] [blame] | 4007 | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) |
| 4008 | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4009 | return Elt; |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4010 | |
| 4011 | return nullptr; |
| 4012 | } |
| 4013 | |
| 4014 | Value *llvm::SimplifyExtractElementInst( |
| 4015 | Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4016 | const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) { |
| 4017 | return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4018 | RecursionLimit); |
| 4019 | } |
| 4020 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4021 | /// See if we can fold the given phi. If not, returns null. |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4022 | static Value *SimplifyPHINode(PHINode *PN, const Query &Q) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4023 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 4024 | // with the common value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4025 | Value *CommonValue = nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4026 | bool HasUndefInput = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 4027 | for (Value *Incoming : PN->incoming_values()) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4028 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 4029 | if (Incoming == PN) continue; |
| 4030 | if (isa<UndefValue>(Incoming)) { |
| 4031 | // Remember that we saw an undef value, but otherwise ignore them. |
| 4032 | HasUndefInput = true; |
| 4033 | continue; |
| 4034 | } |
| 4035 | if (CommonValue && Incoming != CommonValue) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4036 | return nullptr; // Not the same, bail out. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4037 | CommonValue = Incoming; |
| 4038 | } |
| 4039 | |
| 4040 | // If CommonValue is null then all of the incoming values were either undef or |
| 4041 | // equal to the phi node itself. |
| 4042 | if (!CommonValue) |
| 4043 | return UndefValue::get(PN->getType()); |
| 4044 | |
| 4045 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 4046 | // instruction, we cannot return X as the result of the PHI node unless it |
| 4047 | // dominates the PHI block. |
| 4048 | if (HasUndefInput) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4049 | return ValueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr; |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 4050 | |
| 4051 | return CommonValue; |
| 4052 | } |
| 4053 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4054 | static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, |
| 4055 | Type *Ty, const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 126de5d | 2016-07-25 03:39:21 +0000 | [diff] [blame] | 4056 | if (auto *C = dyn_cast<Constant>(Op)) |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4057 | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
Duncan Sands | 395ac42d | 2012-03-13 14:07:05 +0000 | [diff] [blame] | 4058 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4059 | if (auto *CI = dyn_cast<CastInst>(Op)) { |
| 4060 | auto *Src = CI->getOperand(0); |
| 4061 | Type *SrcTy = Src->getType(); |
| 4062 | Type *MidTy = CI->getType(); |
| 4063 | Type *DstTy = Ty; |
| 4064 | if (Src->getType() == Ty) { |
| 4065 | auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode()); |
| 4066 | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
| 4067 | Type *SrcIntPtrTy = |
| 4068 | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr; |
| 4069 | Type *MidIntPtrTy = |
| 4070 | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr; |
| 4071 | Type *DstIntPtrTy = |
| 4072 | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr; |
| 4073 | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
| 4074 | SrcIntPtrTy, MidIntPtrTy, |
| 4075 | DstIntPtrTy) == Instruction::BitCast) |
| 4076 | return Src; |
| 4077 | } |
| 4078 | } |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4079 | |
| 4080 | // bitcast x -> x |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4081 | if (CastOpc == Instruction::BitCast) |
| 4082 | if (Op->getType() == Ty) |
| 4083 | return Op; |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4084 | |
| 4085 | return nullptr; |
| 4086 | } |
| 4087 | |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4088 | Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
| 4089 | const DataLayout &DL, |
| 4090 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4091 | const DominatorTree *DT, AssumptionCache *AC, |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4092 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4093 | return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI), |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4094 | RecursionLimit); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4097 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4098 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4099 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4100 | /// If not, this returns null. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4101 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4102 | const Query &Q, unsigned MaxRecurse) { |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4103 | switch (Opcode) { |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4104 | case Instruction::Add: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 4105 | return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4106 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4107 | case Instruction::FAdd: |
| 4108 | return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4109 | |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4110 | case Instruction::Sub: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 4111 | return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4112 | Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4113 | case Instruction::FSub: |
| 4114 | return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
| 4115 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4116 | case Instruction::Mul: return SimplifyMulInst (LHS, RHS, Q, MaxRecurse); |
Michael Ilseman | d2b05e5 | 2012-12-12 00:29:16 +0000 | [diff] [blame] | 4117 | case Instruction::FMul: |
| 4118 | return SimplifyFMulInst (LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4119 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, Q, MaxRecurse); |
| 4120 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4121 | case Instruction::FDiv: |
| 4122 | return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4123 | case Instruction::SRem: return SimplifySRemInst(LHS, RHS, Q, MaxRecurse); |
| 4124 | case Instruction::URem: return SimplifyURemInst(LHS, RHS, Q, MaxRecurse); |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4125 | case Instruction::FRem: |
| 4126 | return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4127 | case Instruction::Shl: |
Duncan Sands | 8b4e283 | 2011-02-09 17:45:03 +0000 | [diff] [blame] | 4128 | return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4129 | Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4130 | case Instruction::LShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4131 | return SimplifyLShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4132 | case Instruction::AShr: |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4133 | return SimplifyAShrInst(LHS, RHS, /*isExact*/false, Q, MaxRecurse); |
| 4134 | case Instruction::And: return SimplifyAndInst(LHS, RHS, Q, MaxRecurse); |
| 4135 | case Instruction::Or: return SimplifyOrInst (LHS, RHS, Q, MaxRecurse); |
| 4136 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, Q, MaxRecurse); |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4137 | default: |
| 4138 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
Manuel Jacob | a61ca37 | 2016-01-21 06:26:35 +0000 | [diff] [blame] | 4139 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 4140 | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 4141 | |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 4142 | // If the operation is associative, try some generic simplifications. |
| 4143 | if (Instruction::isAssociative(Opcode)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4144 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | 6c7a52c | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 4145 | return V; |
| 4146 | |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4147 | // If the operation is with the result of a select instruction check whether |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 4148 | // operating on either branch of the select always yields the same value. |
Duncan Sands | f64e690 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 4149 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4150 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4151 | return V; |
| 4152 | |
| 4153 | // If the operation is with the result of a phi instruction, check whether |
| 4154 | // 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] | 4155 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4156 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, Q, MaxRecurse)) |
Duncan Sands | b0579e9 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 4157 | return V; |
| 4158 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4159 | return nullptr; |
Chris Lattner | a71e9d6 | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 4160 | } |
| 4161 | } |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4162 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4163 | /// Given operands for a BinaryOperator, see if we can fold the result. |
| 4164 | /// If not, this returns null. |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4165 | /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the |
| 4166 | /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp. |
| 4167 | static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
| 4168 | const FastMathFlags &FMF, const Query &Q, |
| 4169 | unsigned MaxRecurse) { |
| 4170 | switch (Opcode) { |
| 4171 | case Instruction::FAdd: |
| 4172 | return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4173 | case Instruction::FSub: |
| 4174 | return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
| 4175 | case Instruction::FMul: |
| 4176 | return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
Zia Ansari | 394cef8 | 2016-12-08 23:27:40 +0000 | [diff] [blame] | 4177 | case Instruction::FDiv: |
| 4178 | return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse); |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4179 | default: |
| 4180 | return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
| 4181 | } |
| 4182 | } |
| 4183 | |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4184 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4185 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4186 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4187 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4188 | return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4189 | RecursionLimit); |
Chris Lattner | c1f1907 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4192 | Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4193 | const FastMathFlags &FMF, const DataLayout &DL, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4194 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4195 | const DominatorTree *DT, AssumptionCache *AC, |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4196 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4197 | return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI), |
Michael Zolotukhin | 4e8598e | 2015-02-06 20:02:51 +0000 | [diff] [blame] | 4198 | RecursionLimit); |
| 4199 | } |
| 4200 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4201 | /// Given operands for a CmpInst, see if we can fold the result. |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4202 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4203 | const Query &Q, unsigned MaxRecurse) { |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4204 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4205 | return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4206 | return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4210 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4211 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4212 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4213 | return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI), |
Duncan Sands | b8cee00 | 2012-03-13 11:42:19 +0000 | [diff] [blame] | 4214 | RecursionLimit); |
Duncan Sands | f3b1bf1 | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 4215 | } |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4216 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4217 | static bool IsIdempotent(Intrinsic::ID ID) { |
| 4218 | switch (ID) { |
| 4219 | default: return false; |
| 4220 | |
| 4221 | // Unary idempotent: f(f(x)) = f(x) |
| 4222 | case Intrinsic::fabs: |
| 4223 | case Intrinsic::floor: |
| 4224 | case Intrinsic::ceil: |
| 4225 | case Intrinsic::trunc: |
| 4226 | case Intrinsic::rint: |
| 4227 | case Intrinsic::nearbyint: |
Hal Finkel | 171817e | 2013-08-07 22:49:12 +0000 | [diff] [blame] | 4228 | case Intrinsic::round: |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4229 | return true; |
| 4230 | } |
| 4231 | } |
| 4232 | |
Peter Collingbourne | 7dd8dbf | 2016-04-22 21:18:02 +0000 | [diff] [blame] | 4233 | static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
| 4234 | const DataLayout &DL) { |
| 4235 | GlobalValue *PtrSym; |
| 4236 | APInt PtrOffset; |
| 4237 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
| 4238 | return nullptr; |
| 4239 | |
| 4240 | Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext()); |
| 4241 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
| 4242 | Type *Int32PtrTy = Int32Ty->getPointerTo(); |
| 4243 | Type *Int64Ty = Type::getInt64Ty(Ptr->getContext()); |
| 4244 | |
| 4245 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
| 4246 | if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64) |
| 4247 | return nullptr; |
| 4248 | |
| 4249 | uint64_t OffsetInt = OffsetConstInt->getSExtValue(); |
| 4250 | if (OffsetInt % 4 != 0) |
| 4251 | return nullptr; |
| 4252 | |
| 4253 | Constant *C = ConstantExpr::getGetElementPtr( |
| 4254 | Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy), |
| 4255 | ConstantInt::get(Int64Ty, OffsetInt / 4)); |
| 4256 | Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL); |
| 4257 | if (!Loaded) |
| 4258 | return nullptr; |
| 4259 | |
| 4260 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
| 4261 | if (!LoadedCE) |
| 4262 | return nullptr; |
| 4263 | |
| 4264 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
| 4265 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4266 | if (!LoadedCE) |
| 4267 | return nullptr; |
| 4268 | } |
| 4269 | |
| 4270 | if (LoadedCE->getOpcode() != Instruction::Sub) |
| 4271 | return nullptr; |
| 4272 | |
| 4273 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
| 4274 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
| 4275 | return nullptr; |
| 4276 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
| 4277 | |
| 4278 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
| 4279 | GlobalValue *LoadedRHSSym; |
| 4280 | APInt LoadedRHSOffset; |
| 4281 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
| 4282 | DL) || |
| 4283 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
| 4284 | return nullptr; |
| 4285 | |
| 4286 | return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy); |
| 4287 | } |
| 4288 | |
David Majnemer | 17a95aa | 2016-07-14 06:58:37 +0000 | [diff] [blame] | 4289 | static bool maskIsAllZeroOrUndef(Value *Mask) { |
| 4290 | auto *ConstMask = dyn_cast<Constant>(Mask); |
| 4291 | if (!ConstMask) |
| 4292 | return false; |
| 4293 | if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask)) |
| 4294 | return true; |
| 4295 | for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; |
| 4296 | ++I) { |
| 4297 | if (auto *MaskElt = ConstMask->getAggregateElement(I)) |
| 4298 | if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt)) |
| 4299 | continue; |
| 4300 | return false; |
| 4301 | } |
| 4302 | return true; |
| 4303 | } |
| 4304 | |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4305 | template <typename IterTy> |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4306 | static Value *SimplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4307 | const Query &Q, unsigned MaxRecurse) { |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4308 | Intrinsic::ID IID = F->getIntrinsicID(); |
| 4309 | unsigned NumOperands = std::distance(ArgBegin, ArgEnd); |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4310 | |
| 4311 | // Unary Ops |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4312 | if (NumOperands == 1) { |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4313 | // Perform idempotent optimizations |
| 4314 | if (IsIdempotent(IID)) { |
| 4315 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*ArgBegin)) { |
| 4316 | if (II->getIntrinsicID() == IID) |
| 4317 | return II; |
| 4318 | } |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | switch (IID) { |
| 4322 | case Intrinsic::fabs: { |
| 4323 | if (SignBitMustBeZero(*ArgBegin, Q.TLI)) |
| 4324 | return *ArgBegin; |
Marcello Maggioni | 0616b5f | 2017-01-14 07:28:47 +0000 | [diff] [blame] | 4325 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4326 | } |
| 4327 | default: |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4328 | return nullptr; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 4329 | } |
| 4330 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4331 | |
Matt Arsenault | 8260666 | 2017-01-11 00:57:54 +0000 | [diff] [blame] | 4332 | // Binary Ops |
| 4333 | if (NumOperands == 2) { |
| 4334 | Value *LHS = *ArgBegin; |
| 4335 | Value *RHS = *(ArgBegin + 1); |
| 4336 | Type *ReturnType = F->getReturnType(); |
| 4337 | |
| 4338 | switch (IID) { |
| 4339 | case Intrinsic::usub_with_overflow: |
| 4340 | case Intrinsic::ssub_with_overflow: { |
| 4341 | // X - X -> { 0, false } |
| 4342 | if (LHS == RHS) |
| 4343 | return Constant::getNullValue(ReturnType); |
| 4344 | |
| 4345 | // X - undef -> undef |
| 4346 | // undef - X -> undef |
| 4347 | if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) |
| 4348 | return UndefValue::get(ReturnType); |
| 4349 | |
| 4350 | return nullptr; |
| 4351 | } |
| 4352 | case Intrinsic::uadd_with_overflow: |
| 4353 | case Intrinsic::sadd_with_overflow: { |
| 4354 | // X + undef -> undef |
| 4355 | if (isa<UndefValue>(RHS)) |
| 4356 | return UndefValue::get(ReturnType); |
| 4357 | |
| 4358 | return nullptr; |
| 4359 | } |
| 4360 | case Intrinsic::umul_with_overflow: |
| 4361 | case Intrinsic::smul_with_overflow: { |
| 4362 | // X * 0 -> { 0, false } |
| 4363 | if (match(RHS, m_Zero())) |
| 4364 | return Constant::getNullValue(ReturnType); |
| 4365 | |
| 4366 | // X * undef -> { 0, false } |
| 4367 | if (match(RHS, m_Undef())) |
| 4368 | return Constant::getNullValue(ReturnType); |
| 4369 | |
| 4370 | return nullptr; |
| 4371 | } |
| 4372 | case Intrinsic::load_relative: { |
| 4373 | Constant *C0 = dyn_cast<Constant>(LHS); |
| 4374 | Constant *C1 = dyn_cast<Constant>(RHS); |
| 4375 | if (C0 && C1) |
| 4376 | return SimplifyRelativeLoad(C0, C1, Q.DL); |
| 4377 | return nullptr; |
| 4378 | } |
| 4379 | default: |
| 4380 | return nullptr; |
| 4381 | } |
| 4382 | } |
| 4383 | |
| 4384 | // Simplify calls to llvm.masked.load.* |
| 4385 | switch (IID) { |
| 4386 | case Intrinsic::masked_load: { |
| 4387 | Value *MaskArg = ArgBegin[2]; |
| 4388 | Value *PassthruArg = ArgBegin[3]; |
| 4389 | // If the mask is all zeros or undef, the "passthru" argument is the result. |
| 4390 | if (maskIsAllZeroOrUndef(MaskArg)) |
| 4391 | return PassthruArg; |
| 4392 | return nullptr; |
| 4393 | } |
| 4394 | default: |
| 4395 | return nullptr; |
| 4396 | } |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4399 | template <typename IterTy> |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4400 | static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4401 | const Query &Q, unsigned MaxRecurse) { |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4402 | Type *Ty = V->getType(); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4403 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) |
| 4404 | Ty = PTy->getElementType(); |
| 4405 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 4406 | |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4407 | // call undef -> undef |
David Majnemer | bb53d23 | 2016-06-25 07:37:30 +0000 | [diff] [blame] | 4408 | // call null -> undef |
| 4409 | if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V)) |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4410 | return UndefValue::get(FTy->getReturnType()); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4411 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4412 | Function *F = dyn_cast<Function>(V); |
| 4413 | if (!F) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4414 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4415 | |
David Majnemer | 1503258 | 2015-05-22 03:56:46 +0000 | [diff] [blame] | 4416 | if (F->isIntrinsic()) |
| 4417 | if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse)) |
Michael Ilseman | 5485729 | 2013-02-07 19:26:05 +0000 | [diff] [blame] | 4418 | return Ret; |
| 4419 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4420 | if (!canConstantFoldCallTo(F)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4421 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4422 | |
| 4423 | SmallVector<Constant *, 4> ConstantArgs; |
| 4424 | ConstantArgs.reserve(ArgEnd - ArgBegin); |
| 4425 | for (IterTy I = ArgBegin, E = ArgEnd; I != E; ++I) { |
| 4426 | Constant *C = dyn_cast<Constant>(*I); |
| 4427 | if (!C) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 4428 | return nullptr; |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4429 | ConstantArgs.push_back(C); |
| 4430 | } |
| 4431 | |
| 4432 | return ConstantFoldCall(F, ConstantArgs, Q.TLI); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4435 | Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4436 | User::op_iterator ArgEnd, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4437 | const TargetLibraryInfo *TLI, const DominatorTree *DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4438 | AssumptionCache *AC, const Instruction *CxtI) { |
| 4439 | return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI), |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4440 | RecursionLimit); |
| 4441 | } |
| 4442 | |
Chandler Carruth | f618215 | 2012-12-28 14:23:29 +0000 | [diff] [blame] | 4443 | Value *llvm::SimplifyCall(Value *V, ArrayRef<Value *> Args, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4444 | const DataLayout &DL, const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4445 | const DominatorTree *DT, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 4446 | const Instruction *CxtI) { |
| 4447 | return ::SimplifyCall(V, Args.begin(), Args.end(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4448 | Query(DL, TLI, DT, AC, CxtI), RecursionLimit); |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
Sanjay Patel | 472cc78 | 2016-01-11 22:14:42 +0000 | [diff] [blame] | 4451 | /// See if we can compute a simplified version of this instruction. |
| 4452 | /// If not, this returns null. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4453 | Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL, |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 4454 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4455 | const DominatorTree *DT, AssumptionCache *AC) { |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4456 | Value *Result; |
| 4457 | |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4458 | switch (I->getOpcode()) { |
| 4459 | default: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 4460 | Result = ConstantFoldInstruction(I, DL, TLI); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4461 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4462 | case Instruction::FAdd: |
| 4463 | Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4464 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4465 | break; |
Chris Lattner | 3d9823b | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 4466 | case Instruction::Add: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4467 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 4468 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4469 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4470 | TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4471 | break; |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4472 | case Instruction::FSub: |
| 4473 | Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4474 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | bb6f691 | 2012-12-12 00:27:46 +0000 | [diff] [blame] | 4475 | break; |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4476 | case Instruction::Sub: |
| 4477 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 4478 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4479 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4480 | TLI, DT, AC, I); |
Duncan Sands | 0a2c4168 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 4481 | break; |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4482 | case Instruction::FMul: |
| 4483 | Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4484 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Michael Ilseman | be9137a | 2012-11-27 00:46:26 +0000 | [diff] [blame] | 4485 | break; |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4486 | case Instruction::Mul: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4487 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4488 | SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 4489 | break; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4490 | case Instruction::SDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4491 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4492 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4493 | break; |
| 4494 | case Instruction::UDiv: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4495 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4496 | AC, I); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 4497 | break; |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4498 | case Instruction::FDiv: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4499 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4500 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Frits van Bommel | c254966 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 4501 | break; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4502 | case Instruction::SRem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4503 | Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4504 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4505 | break; |
| 4506 | case Instruction::URem: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4507 | Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4508 | AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4509 | break; |
| 4510 | case Instruction::FRem: |
Mehdi Amini | cd3ca6f | 2015-02-23 18:30:25 +0000 | [diff] [blame] | 4511 | Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4512 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 4513 | break; |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4514 | case Instruction::Shl: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4515 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), |
| 4516 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4517 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4518 | TLI, DT, AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4519 | break; |
| 4520 | case Instruction::LShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4521 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4522 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4523 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4524 | break; |
| 4525 | case Instruction::AShr: |
Chris Lattner | 9e4aa02 | 2011-02-09 17:15:04 +0000 | [diff] [blame] | 4526 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4527 | cast<BinaryOperator>(I)->isExact(), DL, TLI, DT, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4528 | AC, I); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 4529 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4530 | case Instruction::And: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4531 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4532 | SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4533 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4534 | case Instruction::Or: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4535 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4536 | SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4537 | break; |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4538 | case Instruction::Xor: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4539 | Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4540 | SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | c89ac07 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 4541 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4542 | case Instruction::ICmp: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4543 | Result = |
| 4544 | SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), I->getOperand(0), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4545 | I->getOperand(1), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4546 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4547 | case Instruction::FCmp: |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4548 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 4549 | I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4550 | I->getFastMathFlags(), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4551 | break; |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 4552 | case Instruction::Select: |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4553 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4554 | I->getOperand(2), DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4555 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4556 | case Instruction::GetElementPtr: { |
| 4557 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 4558 | Result = SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4559 | Ops, DL, TLI, DT, AC, I); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4560 | break; |
Chris Lattner | 8574aba | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 4561 | } |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4562 | case Instruction::InsertValue: { |
| 4563 | InsertValueInst *IV = cast<InsertValueInst>(I); |
| 4564 | Result = SimplifyInsertValueInst(IV->getAggregateOperand(), |
| 4565 | IV->getInsertedValueOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4566 | IV->getIndices(), DL, TLI, DT, AC, I); |
Duncan Sands | fd26a95 | 2011-09-05 06:52:48 +0000 | [diff] [blame] | 4567 | break; |
| 4568 | } |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4569 | case Instruction::ExtractValue: { |
| 4570 | auto *EVI = cast<ExtractValueInst>(I); |
| 4571 | Result = SimplifyExtractValueInst(EVI->getAggregateOperand(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4572 | EVI->getIndices(), DL, TLI, DT, AC, I); |
David Majnemer | 25a796e | 2015-07-13 01:15:46 +0000 | [diff] [blame] | 4573 | break; |
| 4574 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4575 | case Instruction::ExtractElement: { |
| 4576 | auto *EEI = cast<ExtractElementInst>(I); |
| 4577 | Result = SimplifyExtractElementInst( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4578 | EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 4579 | break; |
| 4580 | } |
Duncan Sands | 4581ddc | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 4581 | case Instruction::PHI: |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4582 | Result = SimplifyPHINode(cast<PHINode>(I), Query(DL, TLI, DT, AC, I)); |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4583 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4584 | case Instruction::Call: { |
| 4585 | CallSite CS(cast<CallInst>(I)); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 4586 | Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4587 | TLI, DT, AC, I); |
Dan Gohman | 85977e6 | 2011-11-04 18:32:42 +0000 | [diff] [blame] | 4588 | break; |
Chandler Carruth | 9dc3558 | 2012-12-28 11:30:55 +0000 | [diff] [blame] | 4589 | } |
David Majnemer | 6774d61 | 2016-07-26 17:58:05 +0000 | [diff] [blame] | 4590 | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
| 4591 | #include "llvm/IR/Instruction.def" |
| 4592 | #undef HANDLE_CAST_INST |
| 4593 | Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4594 | DL, TLI, DT, AC, I); |
David Majnemer | a90a621 | 2016-07-26 05:52:29 +0000 | [diff] [blame] | 4595 | break; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4596 | } |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4597 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4598 | // In general, it is possible for computeKnownBits to determine all bits in a |
| 4599 | // value even when the operands are not all constants. |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4600 | if (!Result && I->getType()->isIntOrIntVectorTy()) { |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4601 | unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
| 4602 | APInt KnownZero(BitWidth, 0); |
| 4603 | APInt KnownOne(BitWidth, 0); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4604 | computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4605 | if ((KnownZero | KnownOne).isAllOnesValue()) |
Sanjay Patel | 8ca30ab | 2016-11-27 21:07:28 +0000 | [diff] [blame] | 4606 | Result = ConstantInt::get(I->getType(), KnownOne); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 4609 | /// If called on unreachable code, the above logic may report that the |
| 4610 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | 019a418 | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 4611 | /// detecting that case here, returning a safe value instead. |
| 4612 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | fb7f87d | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Sanjay Patel | f44bd38 | 2016-01-20 18:59:48 +0000 | [diff] [blame] | 4615 | /// \brief Implementation of recursive simplification through an instruction's |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4616 | /// uses. |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4617 | /// |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4618 | /// This is the common implementation of the recursive simplification routines. |
| 4619 | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
| 4620 | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
| 4621 | /// instructions to process and attempt to simplify it using |
| 4622 | /// InstructionSimplify. |
| 4623 | /// |
| 4624 | /// This routine returns 'true' only when *it* simplifies something. The passed |
| 4625 | /// in simplified value does not count toward this. |
| 4626 | static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4627 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4628 | const DominatorTree *DT, |
| 4629 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4630 | bool Simplified = false; |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4631 | SmallSetVector<Instruction *, 8> Worklist; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4632 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4633 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4634 | // If we have an explicit value to collapse to, do that round of the |
| 4635 | // simplification loop by hand initially. |
| 4636 | if (SimpleV) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4637 | for (User *U : I->users()) |
| 4638 | if (U != I) |
| 4639 | Worklist.insert(cast<Instruction>(U)); |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4640 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4641 | // Replace the instruction with its simplified value. |
| 4642 | I->replaceAllUsesWith(SimpleV); |
Chris Lattner | 19eff2a | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 4643 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4644 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4645 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4646 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4647 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4648 | I->eraseFromParent(); |
| 4649 | } else { |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4650 | Worklist.insert(I); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4651 | } |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4652 | |
Chandler Carruth | 77e8bfb | 2012-03-24 22:34:26 +0000 | [diff] [blame] | 4653 | // Note that we must test the size on each iteration, the worklist can grow. |
| 4654 | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { |
| 4655 | I = Worklist[Idx]; |
Duncan Sands | 7e800d6 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 4656 | |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4657 | // See if this instruction simplifies. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4658 | SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4659 | if (!SimpleV) |
| 4660 | continue; |
| 4661 | |
| 4662 | Simplified = true; |
| 4663 | |
| 4664 | // Stash away all the uses of the old instruction so we can check them for |
| 4665 | // recursive simplifications after a RAUW. This is cheaper than checking all |
| 4666 | // uses of To on the recursive step in most cases. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4667 | for (User *U : I->users()) |
| 4668 | Worklist.insert(cast<Instruction>(U)); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4669 | |
| 4670 | // Replace the instruction with its simplified value. |
| 4671 | I->replaceAllUsesWith(SimpleV); |
| 4672 | |
| 4673 | // Gracefully handle edge cases where the instruction is not wired into any |
| 4674 | // parent block. |
David Majnemer | 909793f | 2016-08-04 04:24:02 +0000 | [diff] [blame] | 4675 | if (I->getParent() && !I->isEHPad() && !isa<TerminatorInst>(I) && |
| 4676 | !I->mayHaveSideEffects()) |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4677 | I->eraseFromParent(); |
| 4678 | } |
| 4679 | return Simplified; |
| 4680 | } |
| 4681 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4682 | bool llvm::recursivelySimplifyInstruction(Instruction *I, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4683 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4684 | const DominatorTree *DT, |
| 4685 | AssumptionCache *AC) { |
| 4686 | return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC); |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4690 | const TargetLibraryInfo *TLI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4691 | const DominatorTree *DT, |
| 4692 | AssumptionCache *AC) { |
Chandler Carruth | cf1b585 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 4693 | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
| 4694 | assert(SimpleV && "Must provide a simplified value."); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4695 | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC); |
Chris Lattner | 852d6d6 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 4696 | } |