Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1 | //===- Instructions.cpp - Implement the LLVM instructions -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/None.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/IR/Attributes.h" |
| 21 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 28 | #include "llvm/IR/InstrTypes.h" |
| 29 | #include "llvm/IR/Instruction.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Type.h" |
| 35 | #include "llvm/IR/Value.h" |
| 36 | #include "llvm/Support/AtomicOrdering.h" |
| 37 | #include "llvm/Support/Casting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | #include <cassert> |
| 42 | #include <cstdint> |
| 43 | #include <vector> |
| 44 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // CallSite Class |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 51 | User::op_iterator CallSite::getCallee() const { |
| 52 | Instruction *II(getInstruction()); |
| 53 | return isCall() |
Gabor Greif | 638c823 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 54 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 55 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // TerminatorInst Class |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Reid Kleckner | 45a13e1 | 2017-05-11 21:26:55 +0000 | [diff] [blame] | 62 | unsigned TerminatorInst::getNumSuccessors() const { |
| 63 | switch (getOpcode()) { |
| 64 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 65 | case Instruction::OPC: \ |
Craig Topper | c1993fa | 2017-06-08 23:23:08 +0000 | [diff] [blame] | 66 | return static_cast<const CLASS *>(this)->getNumSuccessors(); |
Reid Kleckner | 45a13e1 | 2017-05-11 21:26:55 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Instruction.def" |
| 68 | default: |
| 69 | break; |
| 70 | } |
| 71 | llvm_unreachable("not a terminator"); |
| 72 | } |
| 73 | |
| 74 | BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const { |
| 75 | switch (getOpcode()) { |
| 76 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 77 | case Instruction::OPC: \ |
Craig Topper | c1993fa | 2017-06-08 23:23:08 +0000 | [diff] [blame] | 78 | return static_cast<const CLASS *>(this)->getSuccessor(idx); |
Reid Kleckner | 45a13e1 | 2017-05-11 21:26:55 +0000 | [diff] [blame] | 79 | #include "llvm/IR/Instruction.def" |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | llvm_unreachable("not a terminator"); |
| 84 | } |
| 85 | |
| 86 | void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) { |
| 87 | switch (getOpcode()) { |
| 88 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 89 | case Instruction::OPC: \ |
Craig Topper | c1993fa | 2017-06-08 23:23:08 +0000 | [diff] [blame] | 90 | return static_cast<CLASS *>(this)->setSuccessor(idx, B); |
Reid Kleckner | 45a13e1 | 2017-05-11 21:26:55 +0000 | [diff] [blame] | 91 | #include "llvm/IR/Instruction.def" |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | llvm_unreachable("not a terminator"); |
| 96 | } |
| 97 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 99 | // SelectInst Class |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
| 102 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 103 | /// for a select operation, otherwise return null. |
| 104 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 105 | if (Op1->getType() != Op2->getType()) |
| 106 | return "both values to select must have same type"; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 107 | |
| 108 | if (Op1->getType()->isTokenTy()) |
| 109 | return "select values cannot have token type"; |
| 110 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 111 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 112 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 113 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 114 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 115 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 116 | if (!ET) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 117 | return "selected values for vector select must be vectors"; |
| 118 | if (ET->getNumElements() != VT->getNumElements()) |
| 119 | return "vector select requires selected vectors to have " |
| 120 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 121 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 122 | return "select condition must be i1 or <n x i1>"; |
| 123 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 124 | return nullptr; |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 128 | // PHINode Class |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | |
| 131 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 132 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 133 | ReservedSpace(PN.getNumOperands()) { |
| 134 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 135 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 136 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 137 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 140 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 141 | // predecessor basic block is deleted. |
| 142 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 143 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 144 | |
| 145 | // Move everything after this operand down. |
| 146 | // |
| 147 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 148 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 150 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 151 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 152 | |
| 153 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 154 | Op<-1>().set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 155 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 156 | |
| 157 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 158 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 159 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 160 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 161 | eraseFromParent(); |
| 162 | } |
| 163 | return Removed; |
| 164 | } |
| 165 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 166 | /// growOperands - grow operands - This grows the operand list in response |
| 167 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 168 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 169 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 170 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 171 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 172 | unsigned NumOps = e + e / 2; |
| 173 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 174 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 175 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 176 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 179 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 180 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 181 | Value *PHINode::hasConstantValue() const { |
| 182 | // Exploit the fact that phi nodes always have at least one entry. |
| 183 | Value *ConstantValue = getIncomingValue(0); |
| 184 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 185 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 186 | if (ConstantValue != this) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 187 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 188 | // The case where the first value is this PHI. |
| 189 | ConstantValue = getIncomingValue(i); |
| 190 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 191 | if (ConstantValue == this) |
| 192 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 193 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Nicolai Haehnle | 13d90f3 | 2016-04-14 17:42:47 +0000 | [diff] [blame] | 196 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges |
| 197 | /// together the same value, assuming that undefs result in the same value as |
| 198 | /// non-undefs. |
| 199 | /// Unlike \ref hasConstantValue, this does not return a value because the |
| 200 | /// unique non-undef incoming value need not dominate the PHI node. |
| 201 | bool PHINode::hasConstantOrUndefValue() const { |
| 202 | Value *ConstantValue = nullptr; |
| 203 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { |
| 204 | Value *Incoming = getIncomingValue(i); |
| 205 | if (Incoming != this && !isa<UndefValue>(Incoming)) { |
| 206 | if (ConstantValue && ConstantValue != Incoming) |
| 207 | return false; |
| 208 | ConstantValue = Incoming; |
| 209 | } |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | // LandingPadInst Implementation |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 218 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 219 | const Twine &NameStr, Instruction *InsertBefore) |
| 220 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 221 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 224 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 225 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 226 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 227 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 231 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 232 | LP.getNumOperands()), |
| 233 | ReservedSpace(LP.getNumOperands()) { |
| 234 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 235 | Use *OL = getOperandList(); |
| 236 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 237 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 238 | OL[I] = InOL[I]; |
| 239 | |
| 240 | setCleanup(LP.isCleanup()); |
| 241 | } |
| 242 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 243 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 244 | const Twine &NameStr, |
| 245 | Instruction *InsertBefore) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 246 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 247 | } |
| 248 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 249 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 250 | const Twine &NameStr, |
| 251 | BasicBlock *InsertAtEnd) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 252 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 253 | } |
| 254 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 255 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 256 | ReservedSpace = NumReservedValues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 257 | setNumHungOffUseOperands(0); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 258 | allocHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 259 | setName(NameStr); |
| 260 | setCleanup(false); |
| 261 | } |
| 262 | |
| 263 | /// growOperands - grow operands - This grows the operand list in response to a |
| 264 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 265 | void LandingPadInst::growOperands(unsigned Size) { |
| 266 | unsigned e = getNumOperands(); |
| 267 | if (ReservedSpace >= e + Size) return; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 268 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 269 | growHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 272 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 273 | unsigned OpNo = getNumOperands(); |
| 274 | growOperands(1); |
| 275 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 276 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 277 | getOperandList()[OpNo] = Val; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 278 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 279 | |
| 280 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 281 | // CallInst Implementation |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 284 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 285 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 286 | this->FTy = FTy; |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 287 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 288 | "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 289 | Op<-1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 290 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 291 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 292 | assert((Args.size() == FTy->getNumParams() || |
| 293 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 294 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 295 | |
| 296 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 297 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 298 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 299 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 300 | #endif |
| 301 | |
| 302 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 303 | |
| 304 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 305 | (void)It; |
| 306 | assert(It + 1 == op_end() && "Should add up!"); |
| 307 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 308 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 311 | void CallInst::init(Value *Func, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 312 | FTy = |
| 313 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 314 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 315 | Op<-1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 316 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 317 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 318 | |
| 319 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Serge Guelton | 1fb81bc | 2018-02-22 13:30:32 +0000 | [diff] [blame] | 322 | CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore) |
| 323 | : CallBase<CallInst>( |
| 324 | cast<FunctionType>( |
| 325 | cast<PointerType>(Func->getType())->getElementType()) |
| 326 | ->getReturnType(), |
| 327 | Instruction::Call, |
| 328 | OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, |
| 329 | InsertBefore) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 330 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Serge Guelton | 1fb81bc | 2018-02-22 13:30:32 +0000 | [diff] [blame] | 333 | CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd) |
| 334 | : CallBase<CallInst>( |
| 335 | cast<FunctionType>( |
| 336 | cast<PointerType>(Func->getType())->getElementType()) |
| 337 | ->getReturnType(), |
| 338 | Instruction::Call, |
| 339 | OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 340 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 343 | CallInst::CallInst(const CallInst &CI) |
Serge Guelton | 1fb81bc | 2018-02-22 13:30:32 +0000 | [diff] [blame] | 344 | : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, |
| 345 | OperandTraits<CallBase<CallInst>>::op_end(this) - |
| 346 | CI.getNumOperands(), |
| 347 | CI.getNumOperands()) { |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 348 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 349 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 350 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 351 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 352 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 353 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 354 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 357 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, |
| 358 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 359 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 360 | |
| 361 | auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(), |
| 362 | InsertPt); |
| 363 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 364 | NewCI->setCallingConv(CI->getCallingConv()); |
| 365 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 366 | NewCI->setAttributes(CI->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 367 | NewCI->setDebugLoc(CI->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 368 | return NewCI; |
| 369 | } |
| 370 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 371 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 372 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 373 | |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 374 | |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 375 | |
Reid Kleckner | 5fbdd17 | 2017-05-31 19:23:09 +0000 | [diff] [blame] | 376 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 377 | |
Amaury Sechet | 1a0e097 | 2016-04-21 21:29:10 +0000 | [diff] [blame] | 378 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 379 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 380 | /// IsConstantOne - Return true only if val is constant int 1 |
| 381 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 382 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 6941785 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 383 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 384 | return CVal && CVal->isOne(); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 387 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 388 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 389 | Type *AllocTy, Value *AllocSize, |
| 390 | Value *ArraySize, |
| 391 | ArrayRef<OperandBundleDef> OpB, |
| 392 | Function *MallocF, const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 393 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 394 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 395 | |
| 396 | // malloc(type) becomes: |
| 397 | // bitcast (i8* malloc(typeSize)) to type* |
| 398 | // malloc(type, arraySize) becomes: |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 399 | // bitcast (i8* malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 400 | if (!ArraySize) |
| 401 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 402 | else if (ArraySize->getType() != IntPtrTy) { |
| 403 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 404 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 405 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 406 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 407 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 408 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 409 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 410 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 411 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 412 | if (IsConstantOne(AllocSize)) { |
| 413 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 414 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 415 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 416 | false /*ZExt*/); |
| 417 | // Malloc arg is constant product of type size and array size |
| 418 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 419 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 420 | // Multiply type size by the array size... |
| 421 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 422 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 423 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 424 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 425 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 426 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 427 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 428 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 429 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 430 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 431 | // Create the call to Malloc. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 432 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 433 | Module *M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 434 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 435 | Value *MallocFunc = MallocF; |
| 436 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 437 | // prototype malloc as "void *malloc(size_t)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 438 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 439 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 440 | CallInst *MCall = nullptr; |
| 441 | Instruction *Result = nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 442 | if (InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 443 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", |
| 444 | InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 445 | Result = MCall; |
| 446 | if (Result->getType() != AllocPtrType) |
| 447 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 448 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 449 | } else { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 450 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 451 | Result = MCall; |
| 452 | if (Result->getType() != AllocPtrType) { |
| 453 | InsertAtEnd->getInstList().push_back(MCall); |
| 454 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 455 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 456 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 457 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 458 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 459 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 460 | MCall->setCallingConv(F->getCallingConv()); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 461 | if (!F->returnDoesNotAlias()) |
| 462 | F->setReturnDoesNotAlias(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 463 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 464 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 465 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 466 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 470 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 471 | /// possibly multiplied by the array size if the array size is not |
| 472 | /// constant 1. |
| 473 | /// 2. Call malloc with that argument. |
| 474 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 475 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 476 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 477 | Value *AllocSize, Value *ArraySize, |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 478 | Function *MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 479 | const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 480 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 481 | ArraySize, None, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 482 | } |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 483 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 484 | Type *IntPtrTy, Type *AllocTy, |
| 485 | Value *AllocSize, Value *ArraySize, |
| 486 | ArrayRef<OperandBundleDef> OpB, |
| 487 | Function *MallocF, |
| 488 | const Twine &Name) { |
| 489 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
| 490 | ArraySize, OpB, MallocF, Name); |
| 491 | } |
| 492 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 493 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 494 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 495 | /// possibly multiplied by the array size if the array size is not |
| 496 | /// constant 1. |
| 497 | /// 2. Call malloc with that argument. |
| 498 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 499 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 500 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 501 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 502 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 503 | Value *AllocSize, Value *ArraySize, |
| 504 | Function *MallocF, const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 505 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 506 | ArraySize, None, MallocF, Name); |
| 507 | } |
| 508 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 509 | Type *IntPtrTy, Type *AllocTy, |
| 510 | Value *AllocSize, Value *ArraySize, |
| 511 | ArrayRef<OperandBundleDef> OpB, |
| 512 | Function *MallocF, const Twine &Name) { |
| 513 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
| 514 | ArraySize, OpB, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 515 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 516 | |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 517 | static Instruction *createFree(Value *Source, |
| 518 | ArrayRef<OperandBundleDef> Bundles, |
| 519 | Instruction *InsertBefore, |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 520 | BasicBlock *InsertAtEnd) { |
| 521 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 522 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 523 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 524 | "Can not free something of nonpointer type!"); |
| 525 | |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 526 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 527 | Module *M = BB->getParent()->getParent(); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 529 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 530 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 531 | // prototype free as "void free(void*)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 532 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 533 | CallInst *Result = nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 534 | Value *PtrCast = Source; |
| 535 | if (InsertBefore) { |
| 536 | if (Source->getType() != IntPtrTy) |
| 537 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 538 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 539 | } else { |
| 540 | if (Source->getType() != IntPtrTy) |
| 541 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 542 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 543 | } |
| 544 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 545 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 546 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 547 | |
| 548 | return Result; |
| 549 | } |
| 550 | |
| 551 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 552 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 553 | return createFree(Source, None, InsertBefore, nullptr); |
| 554 | } |
| 555 | Instruction *CallInst::CreateFree(Value *Source, |
| 556 | ArrayRef<OperandBundleDef> Bundles, |
| 557 | Instruction *InsertBefore) { |
| 558 | return createFree(Source, Bundles, InsertBefore, nullptr); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 562 | /// Note: This function does not add the call to the basic block, that is the |
| 563 | /// responsibility of the caller. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 564 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 565 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); |
| 566 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 567 | return FreeCall; |
| 568 | } |
| 569 | Instruction *CallInst::CreateFree(Value *Source, |
| 570 | ArrayRef<OperandBundleDef> Bundles, |
| 571 | BasicBlock *InsertAtEnd) { |
| 572 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 573 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 574 | return FreeCall; |
| 575 | } |
| 576 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 577 | //===----------------------------------------------------------------------===// |
| 578 | // InvokeInst Implementation |
| 579 | //===----------------------------------------------------------------------===// |
| 580 | |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 581 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 582 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 583 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 584 | const Twine &NameStr) { |
| 585 | this->FTy = FTy; |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 586 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 587 | assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) && |
| 588 | "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 589 | Op<-3>() = Fn; |
| 590 | Op<-2>() = IfNormal; |
| 591 | Op<-1>() = IfException; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 592 | |
| 593 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 594 | assert(((Args.size() == FTy->getNumParams()) || |
| 595 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 596 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 597 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 598 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 599 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 600 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 601 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 602 | #endif |
| 603 | |
| 604 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 605 | |
| 606 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 607 | (void)It; |
| 608 | assert(It + 3 == op_end() && "Should add up!"); |
| 609 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 610 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 613 | InvokeInst::InvokeInst(const InvokeInst &II) |
Serge Guelton | 1fb81bc | 2018-02-22 13:30:32 +0000 | [diff] [blame] | 614 | : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, |
| 615 | OperandTraits<CallBase<InvokeInst>>::op_end(this) - |
| 616 | II.getNumOperands(), |
| 617 | II.getNumOperands()) { |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 618 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 619 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 620 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 621 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 622 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 625 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, |
| 626 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 627 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 628 | |
| 629 | auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(), |
| 630 | II->getUnwindDest(), Args, OpB, |
| 631 | II->getName(), InsertPt); |
| 632 | NewII->setCallingConv(II->getCallingConv()); |
| 633 | NewII->SubclassOptionalData = II->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 634 | NewII->setAttributes(II->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 635 | NewII->setDebugLoc(II->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 636 | return NewII; |
| 637 | } |
| 638 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 639 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 640 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 641 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 642 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 643 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 644 | //===----------------------------------------------------------------------===// |
| 645 | // ReturnInst Implementation |
| 646 | //===----------------------------------------------------------------------===// |
| 647 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 648 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 649 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 650 | OperandTraits<ReturnInst>::op_end(this) - |
| 651 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 652 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 653 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 654 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 655 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 658 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 659 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 660 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 661 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 662 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 663 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 664 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 665 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 666 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 667 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 668 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 669 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 670 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 671 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 672 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 673 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 674 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 675 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 676 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 679 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 680 | // ResumeInst Implementation |
| 681 | //===----------------------------------------------------------------------===// |
| 682 | |
| 683 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 684 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 685 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 686 | Op<0>() = RI.Op<0>(); |
| 687 | } |
| 688 | |
| 689 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 690 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 691 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 692 | Op<0>() = Exn; |
| 693 | } |
| 694 | |
| 695 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 696 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 697 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 698 | Op<0>() = Exn; |
| 699 | } |
| 700 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 701 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 702 | // CleanupReturnInst Implementation |
| 703 | //===----------------------------------------------------------------------===// |
| 704 | |
| 705 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
| 706 | : TerminatorInst(CRI.getType(), Instruction::CleanupRet, |
| 707 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 708 | CRI.getNumOperands(), |
| 709 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 710 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 711 | Op<0>() = CRI.Op<0>(); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 712 | if (CRI.hasUnwindDest()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 713 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 714 | } |
| 715 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 716 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 717 | if (UnwindBB) |
| 718 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 719 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 720 | Op<0>() = CleanupPad; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 721 | if (UnwindBB) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 722 | Op<1>() = UnwindBB; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 723 | } |
| 724 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 725 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 726 | unsigned Values, Instruction *InsertBefore) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 727 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 728 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 729 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 730 | Values, InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 731 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 732 | } |
| 733 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 734 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 735 | unsigned Values, BasicBlock *InsertAtEnd) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 736 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 737 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 738 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 739 | Values, InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 740 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 741 | } |
| 742 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 743 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 744 | // CatchReturnInst Implementation |
| 745 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 746 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 747 | Op<0>() = CatchPad; |
| 748 | Op<1>() = BB; |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 749 | } |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 750 | |
| 751 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
| 752 | : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 753 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
| 754 | Op<0>() = CRI.Op<0>(); |
| 755 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 756 | } |
| 757 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 758 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 759 | Instruction *InsertBefore) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 760 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 761 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 762 | InsertBefore) { |
| 763 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 764 | } |
| 765 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 766 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 767 | BasicBlock *InsertAtEnd) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 768 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 769 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 770 | InsertAtEnd) { |
| 771 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 772 | } |
| 773 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 774 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 775 | // CatchSwitchInst Implementation |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 776 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 777 | |
| 778 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 779 | unsigned NumReservedValues, |
| 780 | const Twine &NameStr, |
| 781 | Instruction *InsertBefore) |
| 782 | : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 783 | InsertBefore) { |
| 784 | if (UnwindDest) |
| 785 | ++NumReservedValues; |
| 786 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 787 | setName(NameStr); |
| 788 | } |
| 789 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 790 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 791 | unsigned NumReservedValues, |
| 792 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 793 | : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 794 | InsertAtEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 795 | if (UnwindDest) |
| 796 | ++NumReservedValues; |
| 797 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
| 798 | setName(NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 799 | } |
| 800 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 801 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) |
| 802 | : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr, |
| 803 | CSI.getNumOperands()) { |
| 804 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); |
| 805 | setNumHungOffUseOperands(ReservedSpace); |
| 806 | Use *OL = getOperandList(); |
| 807 | const Use *InOL = CSI.getOperandList(); |
| 808 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) |
| 809 | OL[I] = InOL[I]; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 810 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 811 | |
| 812 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, |
| 813 | unsigned NumReservedValues) { |
| 814 | assert(ParentPad && NumReservedValues); |
| 815 | |
| 816 | ReservedSpace = NumReservedValues; |
| 817 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); |
| 818 | allocHungoffUses(ReservedSpace); |
| 819 | |
| 820 | Op<0>() = ParentPad; |
| 821 | if (UnwindDest) { |
| 822 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 823 | setUnwindDest(UnwindDest); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /// growOperands - grow operands - This grows the operand list in response to a |
| 828 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 829 | void CatchSwitchInst::growOperands(unsigned Size) { |
| 830 | unsigned NumOperands = getNumOperands(); |
| 831 | assert(NumOperands >= 1); |
| 832 | if (ReservedSpace >= NumOperands + Size) |
| 833 | return; |
| 834 | ReservedSpace = (NumOperands + Size / 2) * 2; |
| 835 | growHungoffUses(ReservedSpace); |
| 836 | } |
| 837 | |
| 838 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { |
| 839 | unsigned OpNo = getNumOperands(); |
| 840 | growOperands(1); |
| 841 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 842 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 843 | getOperandList()[OpNo] = Handler; |
| 844 | } |
| 845 | |
Joseph Tremoulet | 0d80888 | 2016-01-05 02:37:41 +0000 | [diff] [blame] | 846 | void CatchSwitchInst::removeHandler(handler_iterator HI) { |
| 847 | // Move all subsequent handlers up one. |
| 848 | Use *EndDst = op_end() - 1; |
| 849 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) |
| 850 | *CurDst = *(CurDst + 1); |
| 851 | // Null out the last handler use. |
| 852 | *EndDst = nullptr; |
| 853 | |
| 854 | setNumHungOffUseOperands(getNumOperands() - 1); |
| 855 | } |
| 856 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 857 | //===----------------------------------------------------------------------===// |
| 858 | // FuncletPadInst Implementation |
| 859 | //===----------------------------------------------------------------------===// |
| 860 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, |
| 861 | const Twine &NameStr) { |
| 862 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); |
| 863 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 864 | setParentPad(ParentPad); |
| 865 | setName(NameStr); |
| 866 | } |
| 867 | |
| 868 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) |
| 869 | : Instruction(FPI.getType(), FPI.getOpcode(), |
| 870 | OperandTraits<FuncletPadInst>::op_end(this) - |
| 871 | FPI.getNumOperands(), |
| 872 | FPI.getNumOperands()) { |
| 873 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); |
| 874 | setParentPad(FPI.getParentPad()); |
| 875 | } |
| 876 | |
| 877 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 878 | ArrayRef<Value *> Args, unsigned Values, |
| 879 | const Twine &NameStr, Instruction *InsertBefore) |
| 880 | : Instruction(ParentPad->getType(), Op, |
| 881 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 882 | InsertBefore) { |
| 883 | init(ParentPad, Args, NameStr); |
| 884 | } |
| 885 | |
| 886 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 887 | ArrayRef<Value *> Args, unsigned Values, |
| 888 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 889 | : Instruction(ParentPad->getType(), Op, |
| 890 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 891 | InsertAtEnd) { |
| 892 | init(ParentPad, Args, NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 896 | // UnreachableInst Implementation |
| 897 | //===----------------------------------------------------------------------===// |
| 898 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 899 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 900 | Instruction *InsertBefore) |
| 901 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 902 | nullptr, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 903 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 904 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 905 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 906 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 909 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 910 | // BranchInst Implementation |
| 911 | //===----------------------------------------------------------------------===// |
| 912 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 913 | void BranchInst::AssertOK() { |
| 914 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 915 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 916 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 919 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 920 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 921 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 922 | 1, InsertBefore) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 923 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 924 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 925 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 926 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 927 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 928 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 929 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 930 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 931 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 932 | Op<-1>() = IfTrue; |
| 933 | Op<-2>() = IfFalse; |
| 934 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 935 | #ifndef NDEBUG |
| 936 | AssertOK(); |
| 937 | #endif |
| 938 | } |
| 939 | |
| 940 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 941 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 942 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 943 | 1, InsertAtEnd) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 944 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 945 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 949 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 950 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 951 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 952 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 953 | Op<-1>() = IfTrue; |
| 954 | Op<-2>() = IfFalse; |
| 955 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 956 | #ifndef NDEBUG |
| 957 | AssertOK(); |
| 958 | #endif |
| 959 | } |
| 960 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 961 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 962 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 963 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 964 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 965 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 966 | if (BI.getNumOperands() != 1) { |
| 967 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 968 | Op<-3>() = BI.Op<-3>(); |
| 969 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 970 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 971 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 974 | void BranchInst::swapSuccessors() { |
| 975 | assert(isConditional() && |
| 976 | "Cannot swap successors of an unconditional branch"); |
| 977 | Op<-1>().swap(Op<-2>()); |
| 978 | |
| 979 | // Update profile metadata if present and it matches our structural |
| 980 | // expectations. |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 981 | swapProfMetadata(); |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 984 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 985 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 986 | //===----------------------------------------------------------------------===// |
| 987 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 988 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 989 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 990 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 991 | else { |
| 992 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 993 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 994 | assert(Amt->getType()->isIntegerTy() && |
| 995 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 996 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 997 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1000 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1001 | Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1002 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1003 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1004 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1005 | BasicBlock *InsertAtEnd) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1006 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1007 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1008 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1009 | const Twine &Name, Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1010 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
| 1011 | |
| 1012 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1013 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1014 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
| 1015 | |
| 1016 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1017 | unsigned Align, const Twine &Name, |
| 1018 | Instruction *InsertBefore) |
| 1019 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1020 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1021 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1022 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1023 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1024 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1027 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1028 | unsigned Align, const Twine &Name, |
| 1029 | BasicBlock *InsertAtEnd) |
| 1030 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1031 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1032 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1033 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1034 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1035 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1038 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1039 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1040 | assert(Align <= MaximumAlignment && |
| 1041 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1042 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1043 | (Log2_32(Align) + 1)); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1044 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1045 | } |
| 1046 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1047 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1048 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1049 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1050 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1053 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1054 | /// function and is a constant size. If so, the code generator will fold it |
| 1055 | /// into the prolog/epilog code, so it is basically free. |
| 1056 | bool AllocaInst::isStaticAlloca() const { |
| 1057 | // Must be constant size. |
| 1058 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 1059 | |
| 1060 | // Must be in the entry block. |
| 1061 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1062 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1065 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1066 | // LoadInst Implementation |
| 1067 | //===----------------------------------------------------------------------===// |
| 1068 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1069 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1070 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1071 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1072 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1073 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1076 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1077 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1078 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1079 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1080 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1081 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1082 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1083 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1084 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1085 | |
| 1086 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1087 | BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1088 | : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1089 | |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1090 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1091 | unsigned Align, Instruction *InsertBef) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1092 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1093 | SyncScope::System, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1094 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1095 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1096 | unsigned Align, BasicBlock *InsertAE) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1097 | : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1098 | SyncScope::System, InsertAE) {} |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1099 | |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1100 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1101 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1102 | SyncScope::ID SSID, Instruction *InsertBef) |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1103 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | 79009f8 | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1104 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1105 | setVolatile(isVolatile); |
| 1106 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1107 | setAtomic(Order, SSID); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1108 | AssertOK(); |
| 1109 | setName(Name); |
| 1110 | } |
| 1111 | |
| 1112 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1113 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1114 | SyncScope::ID SSID, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1115 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1116 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1117 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1118 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1119 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1120 | setAtomic(Order, SSID); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1121 | AssertOK(); |
| 1122 | setName(Name); |
| 1123 | } |
| 1124 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1125 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 1126 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1127 | Load, Ptr, InsertBef) { |
| 1128 | setVolatile(false); |
| 1129 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1130 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1131 | AssertOK(); |
| 1132 | if (Name && Name[0]) setName(Name); |
| 1133 | } |
| 1134 | |
| 1135 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1136 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1137 | Load, Ptr, InsertAE) { |
| 1138 | setVolatile(false); |
| 1139 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1140 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1141 | AssertOK(); |
| 1142 | if (Name && Name[0]) setName(Name); |
| 1143 | } |
| 1144 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1145 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile, |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1146 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1147 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
| 1148 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1149 | setVolatile(isVolatile); |
| 1150 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1151 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1152 | AssertOK(); |
| 1153 | if (Name && Name[0]) setName(Name); |
| 1154 | } |
| 1155 | |
| 1156 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1157 | BasicBlock *InsertAE) |
| 1158 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1159 | Load, Ptr, InsertAE) { |
| 1160 | setVolatile(isVolatile); |
| 1161 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1162 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1163 | AssertOK(); |
| 1164 | if (Name && Name[0]) setName(Name); |
| 1165 | } |
| 1166 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1167 | void LoadInst::setAlignment(unsigned Align) { |
| 1168 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1169 | assert(Align <= MaximumAlignment && |
| 1170 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1171 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1172 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1173 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1174 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1175 | |
| 1176 | //===----------------------------------------------------------------------===// |
| 1177 | // StoreInst Implementation |
| 1178 | //===----------------------------------------------------------------------===// |
| 1179 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1180 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1181 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1182 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1183 | "Ptr must have pointer type!"); |
| 1184 | assert(getOperand(0)->getType() == |
| 1185 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1186 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1187 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | 1d7c97e | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1188 | "Alignment required for atomic store"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1189 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1191 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1192 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1193 | |
| 1194 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1195 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1196 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1197 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1198 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1199 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1200 | |
| 1201 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1202 | BasicBlock *InsertAtEnd) |
| 1203 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1204 | |
| 1205 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1206 | Instruction *InsertBefore) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1207 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1208 | SyncScope::System, InsertBefore) {} |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1209 | |
| 1210 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1211 | BasicBlock *InsertAtEnd) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1212 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1213 | SyncScope::System, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1214 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1215 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1216 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1217 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1218 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1219 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1220 | OperandTraits<StoreInst>::op_begin(this), |
| 1221 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1222 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1223 | Op<0>() = val; |
| 1224 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1225 | setVolatile(isVolatile); |
| 1226 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1227 | setAtomic(Order, SSID); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1228 | AssertOK(); |
| 1229 | } |
| 1230 | |
| 1231 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1232 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1233 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1234 | BasicBlock *InsertAtEnd) |
| 1235 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1236 | OperandTraits<StoreInst>::op_begin(this), |
| 1237 | OperandTraits<StoreInst>::operands(this), |
| 1238 | InsertAtEnd) { |
| 1239 | Op<0>() = val; |
| 1240 | Op<1>() = addr; |
| 1241 | setVolatile(isVolatile); |
| 1242 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1243 | setAtomic(Order, SSID); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1244 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1247 | void StoreInst::setAlignment(unsigned Align) { |
| 1248 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1249 | assert(Align <= MaximumAlignment && |
| 1250 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1251 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1252 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1253 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1256 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1257 | // AtomicCmpXchgInst Implementation |
| 1258 | //===----------------------------------------------------------------------===// |
| 1259 | |
| 1260 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1261 | AtomicOrdering SuccessOrdering, |
| 1262 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1263 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1264 | Op<0>() = Ptr; |
| 1265 | Op<1>() = Cmp; |
| 1266 | Op<2>() = NewVal; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1267 | setSuccessOrdering(SuccessOrdering); |
| 1268 | setFailureOrdering(FailureOrdering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1269 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1270 | |
| 1271 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1272 | "All operands must be non-null!"); |
| 1273 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1274 | "Ptr must have pointer type!"); |
| 1275 | assert(getOperand(1)->getType() == |
| 1276 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1277 | && "Ptr must be a pointer to Cmp type!"); |
| 1278 | assert(getOperand(2)->getType() == |
| 1279 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1280 | && "Ptr must be a pointer to NewVal type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1281 | assert(SuccessOrdering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1282 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1283 | assert(FailureOrdering != AtomicOrdering::NotAtomic && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1284 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1285 | assert(!isStrongerThan(FailureOrdering, SuccessOrdering) && |
| 1286 | "AtomicCmpXchg failure argument shall be no stronger than the success " |
| 1287 | "argument"); |
| 1288 | assert(FailureOrdering != AtomicOrdering::Release && |
| 1289 | FailureOrdering != AtomicOrdering::AcquireRelease && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1290 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1294 | AtomicOrdering SuccessOrdering, |
| 1295 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1296 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1297 | Instruction *InsertBefore) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1298 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1299 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1300 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1301 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1302 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1306 | AtomicOrdering SuccessOrdering, |
| 1307 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1308 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1309 | BasicBlock *InsertAtEnd) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1310 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1311 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1312 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1313 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1314 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1315 | } |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1316 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1317 | //===----------------------------------------------------------------------===// |
| 1318 | // AtomicRMWInst Implementation |
| 1319 | //===----------------------------------------------------------------------===// |
| 1320 | |
| 1321 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1322 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1323 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1324 | Op<0>() = Ptr; |
| 1325 | Op<1>() = Val; |
| 1326 | setOperation(Operation); |
| 1327 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1328 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1329 | |
| 1330 | assert(getOperand(0) && getOperand(1) && |
| 1331 | "All operands must be non-null!"); |
| 1332 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1333 | "Ptr must have pointer type!"); |
| 1334 | assert(getOperand(1)->getType() == |
| 1335 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1336 | && "Ptr must be a pointer to Val type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1337 | assert(Ordering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1338 | "AtomicRMW instructions must be atomic!"); |
| 1339 | } |
| 1340 | |
| 1341 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1342 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1343 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1344 | Instruction *InsertBefore) |
| 1345 | : Instruction(Val->getType(), AtomicRMW, |
| 1346 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1347 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1348 | InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1349 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1353 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1354 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1355 | BasicBlock *InsertAtEnd) |
| 1356 | : Instruction(Val->getType(), AtomicRMW, |
| 1357 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1358 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1359 | InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1360 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1364 | // FenceInst Implementation |
| 1365 | //===----------------------------------------------------------------------===// |
| 1366 | |
| 1367 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1368 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1369 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1370 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1371 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1372 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1376 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1377 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1378 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1379 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1380 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1384 | // GetElementPtrInst Implementation |
| 1385 | //===----------------------------------------------------------------------===// |
| 1386 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1387 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1388 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1389 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1390 | "NumOperands not initialized?"); |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1391 | Op<0>() = Ptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1392 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1393 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1396 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1397 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1398 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1399 | GEPI.getNumOperands(), |
| 1400 | GEPI.getNumOperands()), |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1401 | SourceElementType(GEPI.SourceElementType), |
| 1402 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1403 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1404 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1407 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1408 | /// a gep instruction with the specified parameters. |
| 1409 | /// |
| 1410 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1411 | /// indices, either as Value* or uint64_t. |
| 1412 | /// |
| 1413 | /// A null type is returned if the indices are invalid for the specified |
| 1414 | /// pointer type. |
| 1415 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1416 | template <typename IndexTy> |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1417 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1418 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1419 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1420 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1422 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1423 | // it cannot be 'stepped over'. |
| 1424 | if (!Agg->isSized()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1425 | return nullptr; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1426 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1427 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1428 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1429 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1430 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1431 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1432 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1433 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1434 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1435 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1438 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1439 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1442 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1443 | ArrayRef<Constant *> IdxList) { |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1444 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1447 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1448 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1451 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1452 | /// zeros. If so, the result pointer and the first operand have the same |
| 1453 | /// value, just potentially different types. |
| 1454 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1455 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1456 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1457 | if (!CI->isZero()) return false; |
| 1458 | } else { |
| 1459 | return false; |
| 1460 | } |
| 1461 | } |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1465 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1466 | /// constant integers. If so, the result pointer and the first operand have |
| 1467 | /// a constant offset between them. |
| 1468 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1469 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1470 | if (!isa<ConstantInt>(getOperand(i))) |
| 1471 | return false; |
| 1472 | } |
| 1473 | return true; |
| 1474 | } |
| 1475 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1476 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1477 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1478 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1479 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1480 | bool GetElementPtrInst::isInBounds() const { |
| 1481 | return cast<GEPOperator>(this)->isInBounds(); |
| 1482 | } |
| 1483 | |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1484 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1485 | APInt &Offset) const { |
Chandler Carruth | 7ec41c7 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1486 | // Delegate to the generic GEPOperator implementation. |
| 1487 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1490 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1491 | // ExtractElementInst Implementation |
| 1492 | //===----------------------------------------------------------------------===// |
| 1493 | |
| 1494 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1495 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1496 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1497 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1498 | ExtractElement, |
| 1499 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1500 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1501 | assert(isValidOperands(Val, Index) && |
| 1502 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1503 | Op<0>() = Val; |
| 1504 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1505 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1509 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1510 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1511 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1512 | ExtractElement, |
| 1513 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1514 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1515 | assert(isValidOperands(Val, Index) && |
| 1516 | "Invalid extractelement instruction operands!"); |
| 1517 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1518 | Op<0>() = Val; |
| 1519 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1520 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1523 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1524 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1525 | return false; |
| 1526 | return true; |
| 1527 | } |
| 1528 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1529 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1530 | // InsertElementInst Implementation |
| 1531 | //===----------------------------------------------------------------------===// |
| 1532 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1533 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1534 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1535 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1536 | : Instruction(Vec->getType(), InsertElement, |
| 1537 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1538 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1539 | assert(isValidOperands(Vec, Elt, Index) && |
| 1540 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1541 | Op<0>() = Vec; |
| 1542 | Op<1>() = Elt; |
| 1543 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1544 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1547 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1548 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1549 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1550 | : Instruction(Vec->getType(), InsertElement, |
| 1551 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1552 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1553 | assert(isValidOperands(Vec, Elt, Index) && |
| 1554 | "Invalid insertelement instruction operands!"); |
| 1555 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1556 | Op<0>() = Vec; |
| 1557 | Op<1>() = Elt; |
| 1558 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1559 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1562 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1563 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1564 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1565 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1566 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1567 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1568 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1569 | |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1570 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1571 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1572 | return true; |
| 1573 | } |
| 1574 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1575 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1576 | // ShuffleVectorInst Implementation |
| 1577 | //===----------------------------------------------------------------------===// |
| 1578 | |
| 1579 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1580 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1581 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1582 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1583 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1584 | ShuffleVector, |
| 1585 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1586 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1587 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1588 | assert(isValidOperands(V1, V2, Mask) && |
| 1589 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1590 | Op<0>() = V1; |
| 1591 | Op<1>() = V2; |
| 1592 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1593 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1597 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1598 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1599 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1600 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1601 | ShuffleVector, |
| 1602 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1603 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1604 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1605 | assert(isValidOperands(V1, V2, Mask) && |
| 1606 | "Invalid shuffle vector instruction operands!"); |
| 1607 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1608 | Op<0>() = V1; |
| 1609 | Op<1>() = V2; |
| 1610 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1611 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1614 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1615 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1616 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1617 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1618 | return false; |
| 1619 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1620 | // Mask must be vector of i32. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1621 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1622 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1623 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1626 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1627 | return true; |
| 1628 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1629 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1630 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1631 | for (Value *Op : MV->operands()) { |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1632 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1633 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1634 | return false; |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1635 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1636 | return false; |
| 1637 | } |
| 1638 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1639 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1640 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1641 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1642 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1643 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1644 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1645 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1646 | return false; |
| 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | // The bitcode reader can create a place holder for a forward reference |
| 1651 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1652 | // fall into this case and fail. To avoid this error, do this bit of |
| 1653 | // ugliness to allow such a mask pass. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1654 | if (const auto *CE = dyn_cast<ConstantExpr>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1655 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1656 | return true; |
| 1657 | |
| 1658 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1661 | int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { |
| 1662 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1663 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1664 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1665 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1666 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1667 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1668 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1671 | void ShuffleVectorInst::getShuffleMask(Constant *Mask, |
| 1672 | SmallVectorImpl<int> &Result) { |
| 1673 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1674 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1675 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1676 | for (unsigned i = 0; i != NumElts; ++i) |
| 1677 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1678 | return; |
| 1679 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1680 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1681 | Constant *C = Mask->getAggregateElement(i); |
| 1682 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1683 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1687 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1688 | // InsertValueInst Class |
| 1689 | //===----------------------------------------------------------------------===// |
| 1690 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1691 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1692 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1693 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1694 | |
| 1695 | // There's no fundamental reason why we require at least one index |
| 1696 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1697 | // getelementptr's init routine for example). But there's no |
| 1698 | // present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1699 | assert(!Idxs.empty() && "InsertValueInst must have at least one index"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1700 | |
| 1701 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1702 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1703 | Op<0>() = Agg; |
| 1704 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1705 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1706 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1707 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1711 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1712 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1713 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1714 | Op<0>() = IVI.getOperand(0); |
| 1715 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1716 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1720 | // ExtractValueInst Class |
| 1721 | //===----------------------------------------------------------------------===// |
| 1722 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1723 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1724 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1725 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1726 | // There's no fundamental reason why we require at least one index. |
| 1727 | // But there's no present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1728 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1729 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1730 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1731 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1735 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1736 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1737 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1740 | // getIndexedType - Returns the type of the element that would be extracted |
| 1741 | // with an extractvalue instruction with the specified parameters. |
| 1742 | // |
| 1743 | // A null type is returned if the indices are invalid for the specified |
| 1744 | // pointer type. |
| 1745 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1746 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1747 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1748 | for (unsigned Index : Idxs) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1749 | // We can't use CompositeType::indexValid(Index) here. |
| 1750 | // indexValid() always returns true for arrays because getelementptr allows |
| 1751 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1752 | // insertvalue we need to check array indexing manually. |
| 1753 | // Since the only other types we can index into are struct types it's just |
| 1754 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1755 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1756 | if (Index >= AT->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1757 | return nullptr; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1758 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1759 | if (Index >= ST->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1760 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1761 | } else { |
| 1762 | // Not a valid type to index into. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1763 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1767 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1768 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1769 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1770 | |
| 1771 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1772 | // BinaryOperator Class |
| 1773 | //===----------------------------------------------------------------------===// |
| 1774 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1775 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1776 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1777 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1778 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1779 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1780 | OperandTraits<BinaryOperator>::operands(this), |
| 1781 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1782 | Op<0>() = S1; |
| 1783 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1784 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 1785 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1789 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1790 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1791 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1792 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1793 | OperandTraits<BinaryOperator>::operands(this), |
| 1794 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1795 | Op<0>() = S1; |
| 1796 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1797 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 1798 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 1801 | void BinaryOperator::AssertOK() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1802 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 1803 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1804 | assert(LHS->getType() == RHS->getType() && |
| 1805 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1806 | #ifndef NDEBUG |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 1807 | switch (getOpcode()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1808 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1809 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1810 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1811 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1812 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1813 | "Tried to create an integer operation on a non-integer type!"); |
| 1814 | break; |
| 1815 | case FAdd: case FSub: |
| 1816 | case FMul: |
| 1817 | assert(getType() == LHS->getType() && |
| 1818 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1819 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1820 | "Tried to create a floating-point operation on a " |
| 1821 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1822 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1823 | case UDiv: |
| 1824 | case SDiv: |
| 1825 | assert(getType() == LHS->getType() && |
| 1826 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 1827 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1828 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1829 | break; |
| 1830 | case FDiv: |
| 1831 | assert(getType() == LHS->getType() && |
| 1832 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1833 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1834 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1835 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1836 | case URem: |
| 1837 | case SRem: |
| 1838 | assert(getType() == LHS->getType() && |
| 1839 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 1840 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1841 | "Incorrect operand type (not integer) for S/UREM"); |
| 1842 | break; |
| 1843 | case FRem: |
| 1844 | assert(getType() == LHS->getType() && |
| 1845 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1846 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1847 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1848 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1849 | case Shl: |
| 1850 | case LShr: |
| 1851 | case AShr: |
| 1852 | assert(getType() == LHS->getType() && |
| 1853 | "Shift operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 1854 | assert(getType()->isIntOrIntVectorTy() && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1855 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1856 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1857 | case And: case Or: |
| 1858 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1859 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1860 | "Logical operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 1861 | assert(getType()->isIntOrIntVectorTy() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1862 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1863 | break; |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 1864 | default: llvm_unreachable("Invalid opcode provided"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1865 | } |
| 1866 | #endif |
| 1867 | } |
| 1868 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1869 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1870 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1871 | Instruction *InsertBefore) { |
| 1872 | assert(S1->getType() == S2->getType() && |
| 1873 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1874 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1877 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1878 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1879 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1880 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1881 | InsertAtEnd->getInstList().push_back(Res); |
| 1882 | return Res; |
| 1883 | } |
| 1884 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1885 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1886 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1887 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1888 | return new BinaryOperator(Instruction::Sub, |
| 1889 | zero, Op, |
| 1890 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1893 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1894 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1895 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1896 | return new BinaryOperator(Instruction::Sub, |
| 1897 | zero, Op, |
| 1898 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 1901 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1902 | Instruction *InsertBefore) { |
| 1903 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1904 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 1905 | } |
| 1906 | |
| 1907 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1908 | BasicBlock *InsertAtEnd) { |
| 1909 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1910 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 1911 | } |
| 1912 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 1913 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1914 | Instruction *InsertBefore) { |
| 1915 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1916 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 1917 | } |
| 1918 | |
| 1919 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1920 | BasicBlock *InsertAtEnd) { |
| 1921 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1922 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 1923 | } |
| 1924 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1925 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1926 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1927 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1928 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1929 | Op->getType(), Name, InsertBefore); |
| 1930 | } |
| 1931 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1932 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1933 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1934 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1935 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1936 | Op->getType(), Name, InsertAtEnd); |
| 1937 | } |
| 1938 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1939 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1940 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1941 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1942 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1943 | Op->getType(), Name, InsertBefore); |
| 1944 | } |
| 1945 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1946 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1947 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1948 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1949 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1950 | Op->getType(), Name, InsertAtEnd); |
| 1951 | } |
| 1952 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1953 | // isConstantAllOnes - Helper function for several functions below |
| 1954 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1955 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 1956 | return C->isAllOnesValue(); |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1957 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1960 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1961 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1962 | if (Bop->getOpcode() == Instruction::Sub) |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 1963 | if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1964 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1965 | return false; |
| 1966 | } |
| 1967 | |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 1968 | bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1969 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1970 | if (Bop->getOpcode() == Instruction::FSub) |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 1971 | if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) { |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 1972 | if (!IgnoreZeroSign) |
| 1973 | IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros(); |
| 1974 | return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); |
| 1975 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1976 | return false; |
| 1977 | } |
| 1978 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1979 | bool BinaryOperator::isNot(const Value *V) { |
| 1980 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1981 | return (Bop->getOpcode() == Instruction::Xor && |
| 1982 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1983 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1987 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1988 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1991 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1992 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1995 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1996 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 1997 | } |
| 1998 | |
| 1999 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 2000 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 2001 | } |
| 2002 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2003 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 2004 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 2005 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 2006 | Value *Op0 = BO->getOperand(0); |
| 2007 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2008 | if (isConstantAllOnes(Op0)) return Op1; |
| 2009 | |
| 2010 | assert(isConstantAllOnes(Op1)); |
| 2011 | return Op0; |
| 2012 | } |
| 2013 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2014 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 2015 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Sanjay Patel | 4ce99d4 | 2016-11-16 18:09:44 +0000 | [diff] [blame] | 2018 | // Exchange the two operands to this instruction. This instruction is safe to |
| 2019 | // use on any binary instruction and does not modify the semantics of the |
| 2020 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode |
| 2021 | // is changed. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2022 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2023 | if (!isCommutative()) |
| 2024 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2025 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2026 | return false; |
| 2027 | } |
| 2028 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2029 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2030 | // FPMathOperator Class |
| 2031 | //===----------------------------------------------------------------------===// |
| 2032 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2033 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2034 | const MDNode *MD = |
| 2035 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2036 | if (!MD) |
| 2037 | return 0.0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2038 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2039 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2042 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2043 | // CastInst Class |
| 2044 | //===----------------------------------------------------------------------===// |
| 2045 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2046 | // Just determine if this cast only deals with integral->integral conversion. |
| 2047 | bool CastInst::isIntegerCast() const { |
| 2048 | switch (getOpcode()) { |
| 2049 | default: return false; |
| 2050 | case Instruction::ZExt: |
| 2051 | case Instruction::SExt: |
| 2052 | case Instruction::Trunc: |
| 2053 | return true; |
| 2054 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2055 | return getOperand(0)->getType()->isIntegerTy() && |
| 2056 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2057 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2060 | bool CastInst::isLosslessCast() const { |
| 2061 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2062 | if (getOpcode() != Instruction::BitCast) |
| 2063 | return false; |
| 2064 | |
| 2065 | // Identity cast is always lossless |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2066 | Type *SrcTy = getOperand(0)->getType(); |
| 2067 | Type *DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2068 | if (SrcTy == DstTy) |
| 2069 | return true; |
| 2070 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2071 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2072 | if (SrcTy->isPointerTy()) |
| 2073 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2074 | return false; // Other types have no identity values |
| 2075 | } |
| 2076 | |
| 2077 | /// This function determines if the CastInst does not require any bits to be |
| 2078 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 2079 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 2080 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2081 | /// # bitcast i32* %x to i8* |
| 2082 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 2083 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2084 | /// @brief Determine if the described cast is a no-op. |
| 2085 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2086 | Type *SrcTy, |
| 2087 | Type *DestTy, |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2088 | const DataLayout &DL) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2089 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2090 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2091 | case Instruction::Trunc: |
| 2092 | case Instruction::ZExt: |
| 2093 | case Instruction::SExt: |
| 2094 | case Instruction::FPTrunc: |
| 2095 | case Instruction::FPExt: |
| 2096 | case Instruction::UIToFP: |
| 2097 | case Instruction::SIToFP: |
| 2098 | case Instruction::FPToUI: |
| 2099 | case Instruction::FPToSI: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2100 | case Instruction::AddrSpaceCast: |
| 2101 | // TODO: Target informations may give a more accurate answer here. |
| 2102 | return false; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2103 | case Instruction::BitCast: |
| 2104 | return true; // BitCast never modifies bits. |
| 2105 | case Instruction::PtrToInt: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2106 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2107 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2108 | case Instruction::IntToPtr: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2109 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2110 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2114 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Mikael Holmen | 6efe507 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 2115 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | /// This function determines if a pair of casts can be eliminated and what |
| 2119 | /// opcode should be used in the elimination. This assumes that there are two |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2120 | /// instructions like this: |
| 2121 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2122 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2123 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2124 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2125 | /// If no such cast is permitted, the function returns 0. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2126 | unsigned CastInst::isEliminableCastPair( |
| 2127 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2128 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2129 | Type *DstIntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2130 | // Define the 144 possibilities for these two cast instructions. The values |
| 2131 | // in this matrix determine what to do in a given situation and select the |
| 2132 | // case in the switch below. The rows correspond to firstOp, the columns |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2133 | // correspond to secondOp. In looking at the table below, keep in mind |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2134 | // the following cast properties: |
| 2135 | // |
| 2136 | // Size Compare Source Destination |
| 2137 | // Operator Src ? Size Type Sign Type Sign |
| 2138 | // -------- ------------ ------------------- --------------------- |
| 2139 | // TRUNC > Integer Any Integral Any |
| 2140 | // ZEXT < Integral Unsigned Integer Any |
| 2141 | // SEXT < Integral Signed Integer Any |
| 2142 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2143 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2144 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2145 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2146 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2147 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2148 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2149 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2150 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2151 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2152 | // |
| 2153 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2154 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2155 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2156 | // of the produced value (we no longer know the top-part is all zeros). |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2157 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2158 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2159 | // same reason. |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2160 | const unsigned numCastOps = |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2161 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2162 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2163 | // T F F U S F F P I B A -+ |
| 2164 | // R Z S P P I I T P 2 N T S | |
| 2165 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2166 | // N X X U S F F N X N 2 V V | |
| 2167 | // C T T I I P P C T T P T T -+ |
| 2168 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2169 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2170 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2171 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2172 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2173 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2174 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | |
Ahmed Bougacha | 0ea9d1e | 2015-05-29 00:04:30 +0000 | [diff] [blame] | 2175 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | |
Craig Topper | 18799f4 | 2018-03-02 18:16:51 +0000 | [diff] [blame] | 2176 | { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2177 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2178 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2179 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2180 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2181 | }; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2182 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2183 | // TODO: This logic could be encoded into the table above and handled in the |
| 2184 | // switch below. |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2185 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2186 | // merging. However, any pair of bitcasts are allowed. |
| 2187 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); |
| 2188 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); |
| 2189 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2190 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2191 | // Check if any of the casts convert scalars <-> vectors. |
| 2192 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2193 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2194 | if (!AreBothBitcasts) |
| 2195 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2196 | |
| 2197 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2198 | [secondOp-Instruction::CastOpsBegin]; |
| 2199 | switch (ElimCase) { |
| 2200 | case 0: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2201 | // Categorically disallowed. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2202 | return 0; |
| 2203 | case 1: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2204 | // Allowed, use first cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2205 | return firstOp; |
| 2206 | case 2: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2207 | // Allowed, use second cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2208 | return secondOp; |
| 2209 | case 3: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2210 | // No-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2211 | // is integer and we are not converting between a vector and a |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2212 | // non-vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2213 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2214 | return firstOp; |
| 2215 | return 0; |
| 2216 | case 4: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2217 | // No-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2218 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2219 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2220 | return firstOp; |
| 2221 | return 0; |
| 2222 | case 5: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2223 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2224 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2225 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2226 | return secondOp; |
| 2227 | return 0; |
| 2228 | case 6: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2229 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2230 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2231 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2232 | return secondOp; |
| 2233 | return 0; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2234 | case 7: { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2235 | // Cannot simplify if address spaces are different! |
| 2236 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2237 | return 0; |
| 2238 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2239 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2240 | // We can still fold this without knowing the actual sizes as long we |
| 2241 | // know that the intermediate pointer is the largest possible |
| 2242 | // pointer size. |
| 2243 | // FIXME: Is this always true? |
| 2244 | if (MidSize == 64) |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2245 | return Instruction::BitCast; |
| 2246 | |
| 2247 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2248 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2249 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2250 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2251 | if (MidSize >= PtrSize) |
| 2252 | return Instruction::BitCast; |
| 2253 | return 0; |
| 2254 | } |
| 2255 | case 8: { |
| 2256 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2257 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2258 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2259 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2260 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2261 | if (SrcSize == DstSize) |
| 2262 | return Instruction::BitCast; |
| 2263 | else if (SrcSize < DstSize) |
| 2264 | return firstOp; |
| 2265 | return secondOp; |
| 2266 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2267 | case 9: |
| 2268 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2269 | return Instruction::ZExt; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2270 | case 11: { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2271 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2272 | if (!MidIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2273 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2274 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2275 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2276 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2277 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2278 | return Instruction::BitCast; |
| 2279 | return 0; |
| 2280 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2281 | case 12: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2282 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2283 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2284 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2285 | return Instruction::AddrSpaceCast; |
| 2286 | return Instruction::BitCast; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2287 | case 13: |
| 2288 | // FIXME: this state can be merged with (1), but the following assert |
| 2289 | // is useful to check the correcteness of the sequence due to semantic |
| 2290 | // change of bitcast. |
| 2291 | assert( |
| 2292 | SrcTy->isPtrOrPtrVectorTy() && |
| 2293 | MidTy->isPtrOrPtrVectorTy() && |
| 2294 | DstTy->isPtrOrPtrVectorTy() && |
| 2295 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2296 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2297 | "Illegal addrspacecast, bitcast sequence!"); |
| 2298 | // Allowed, use first cast's opcode |
| 2299 | return firstOp; |
| 2300 | case 14: |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2301 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2302 | // bitcast's source is the same as that of addrspacecast's destination. |
Peter Collingbourne | 9d5fd4d | 2016-11-13 06:58:45 +0000 | [diff] [blame] | 2303 | if (SrcTy->getScalarType()->getPointerElementType() == |
| 2304 | DstTy->getScalarType()->getPointerElementType()) |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2305 | return Instruction::AddrSpaceCast; |
| 2306 | return 0; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2307 | case 15: |
| 2308 | // FIXME: this state can be merged with (1), but the following assert |
| 2309 | // is useful to check the correcteness of the sequence due to semantic |
| 2310 | // change of bitcast. |
| 2311 | assert( |
| 2312 | SrcTy->isIntOrIntVectorTy() && |
| 2313 | MidTy->isPtrOrPtrVectorTy() && |
| 2314 | DstTy->isPtrOrPtrVectorTy() && |
| 2315 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2316 | "Illegal inttoptr, bitcast sequence!"); |
| 2317 | // Allowed, use first cast's opcode |
| 2318 | return firstOp; |
| 2319 | case 16: |
| 2320 | // FIXME: this state can be merged with (2), but the following assert |
| 2321 | // is useful to check the correcteness of the sequence due to semantic |
| 2322 | // change of bitcast. |
| 2323 | assert( |
| 2324 | SrcTy->isPtrOrPtrVectorTy() && |
| 2325 | MidTy->isPtrOrPtrVectorTy() && |
| 2326 | DstTy->isIntOrIntVectorTy() && |
| 2327 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2328 | "Illegal bitcast, ptrtoint sequence!"); |
| 2329 | // Allowed, use second cast's opcode |
| 2330 | return secondOp; |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2331 | case 17: |
| 2332 | // (sitofp (zext x)) -> (uitofp x) |
| 2333 | return Instruction::UIToFP; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2334 | case 99: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2335 | // Cast combination can't happen (error in input). This is for all cases |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2336 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2337 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2338 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2339 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2340 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2343 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2344 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2345 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2346 | // Construct and return the appropriate CastInst subclass |
| 2347 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2348 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2349 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2350 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2351 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2352 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2353 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2354 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2355 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2356 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2357 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2358 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2359 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2360 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2361 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2362 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2365 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2366 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2367 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2368 | // Construct and return the appropriate CastInst subclass |
| 2369 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2370 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2371 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2372 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2373 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2374 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2375 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2376 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2377 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2378 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2379 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2380 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2381 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2382 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2383 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2384 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2387 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2388 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2389 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2390 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2391 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2392 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2395 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2396 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2397 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2398 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2399 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2400 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2403 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2404 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2405 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2406 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2407 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2408 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2411 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2412 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2413 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2414 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2415 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2416 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2419 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2420 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2421 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2422 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2423 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2424 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2427 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2428 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2429 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2430 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2431 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2432 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2435 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2436 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2437 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2438 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2439 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2440 | "Invalid cast"); |
| 2441 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2442 | assert((!Ty->isVectorTy() || |
| 2443 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2444 | "Invalid cast"); |
| 2445 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2446 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2447 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2448 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2449 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2453 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2454 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2455 | Instruction *InsertBefore) { |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2456 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2457 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2458 | "Invalid cast"); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2459 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2460 | assert((!Ty->isVectorTy() || |
| 2461 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2462 | "Invalid cast"); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2463 | |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2464 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2465 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2466 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2467 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2468 | } |
| 2469 | |
| 2470 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2471 | Value *S, Type *Ty, |
| 2472 | const Twine &Name, |
| 2473 | BasicBlock *InsertAtEnd) { |
| 2474 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2475 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2476 | |
| 2477 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2478 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2479 | |
| 2480 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2481 | } |
| 2482 | |
| 2483 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2484 | Value *S, Type *Ty, |
| 2485 | const Twine &Name, |
| 2486 | Instruction *InsertBefore) { |
| 2487 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2488 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2489 | |
| 2490 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2491 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2492 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2493 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2496 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2497 | const Twine &Name, |
| 2498 | Instruction *InsertBefore) { |
| 2499 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2500 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2501 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2502 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2503 | |
| 2504 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2505 | } |
| 2506 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2507 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2508 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2509 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2510 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2511 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2512 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2513 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2514 | Instruction::CastOps opcode = |
| 2515 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2516 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2517 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2518 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2521 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2522 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2523 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2524 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2525 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2526 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2527 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2528 | Instruction::CastOps opcode = |
| 2529 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2530 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2531 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2532 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2535 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2536 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2537 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2538 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2539 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2540 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2541 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2542 | Instruction::CastOps opcode = |
| 2543 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2544 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2545 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2548 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2549 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2550 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2551 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2552 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2553 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2554 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2555 | Instruction::CastOps opcode = |
| 2556 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2557 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2558 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2561 | // Check whether it is valid to call getCastOpcode for these types. |
| 2562 | // This routine must be kept in sync with getCastOpcode. |
| 2563 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2564 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2565 | return false; |
| 2566 | |
| 2567 | if (SrcTy == DestTy) |
| 2568 | return true; |
| 2569 | |
| 2570 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2571 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2572 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2573 | // An element by element cast. Valid if casting the elements is valid. |
| 2574 | SrcTy = SrcVecTy->getElementType(); |
| 2575 | DestTy = DestVecTy->getElementType(); |
| 2576 | } |
| 2577 | |
| 2578 | // Get the bit sizes, we'll need these |
| 2579 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2580 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2581 | |
| 2582 | // Run through the possibilities ... |
| 2583 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2584 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2585 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2586 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2587 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2588 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2589 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2590 | // Casting from something else |
| 2591 | return SrcTy->isPointerTy(); |
| 2592 | } |
| 2593 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2594 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2595 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2596 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2597 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2598 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2599 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2600 | // Casting from something else |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2601 | return false; |
| 2602 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2603 | if (DestTy->isVectorTy()) // Casting to vector |
| 2604 | return DestBits == SrcBits; |
| 2605 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2606 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2607 | return true; |
| 2608 | return SrcTy->isIntegerTy(); // Casting from integral |
| 2609 | } |
| 2610 | if (DestTy->isX86_MMXTy()) { |
| 2611 | if (SrcTy->isVectorTy()) |
| 2612 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2613 | return false; |
| 2614 | } // Casting to something else |
| 2615 | return false; |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2618 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2619 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2620 | return false; |
| 2621 | |
| 2622 | if (SrcTy == DestTy) |
| 2623 | return true; |
| 2624 | |
| 2625 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2626 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 2627 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2628 | // An element by element cast. Valid if casting the elements is valid. |
| 2629 | SrcTy = SrcVecTy->getElementType(); |
| 2630 | DestTy = DestVecTy->getElementType(); |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 2636 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 2637 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2642 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2643 | |
| 2644 | // Could still have vectors of pointers if the number of elements doesn't |
| 2645 | // match |
| 2646 | if (SrcBits == 0 || DestBits == 0) |
| 2647 | return false; |
| 2648 | |
| 2649 | if (SrcBits != DestBits) |
| 2650 | return false; |
| 2651 | |
| 2652 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 2653 | return false; |
| 2654 | |
| 2655 | return true; |
| 2656 | } |
| 2657 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2658 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2659 | const DataLayout &DL) { |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2660 | // ptrtoint and inttoptr are not allowed on non-integral pointers |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2661 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 2662 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2663 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2664 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2665 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 2666 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2667 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2668 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2669 | |
| 2670 | return isBitCastable(SrcTy, DestTy); |
| 2671 | } |
| 2672 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2673 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2674 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2675 | // logic in the castIsValid function below. This axiom should hold: |
| 2676 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2677 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2678 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2679 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2680 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2681 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2682 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2683 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2684 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2685 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2686 | "Only first class types are castable!"); |
| 2687 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2688 | if (SrcTy == DestTy) |
| 2689 | return BitCast; |
| 2690 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2691 | // FIXME: Check address space sizes here |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2692 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2693 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2694 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2695 | // An element by element cast. Find the appropriate opcode based on the |
| 2696 | // element types. |
| 2697 | SrcTy = SrcVecTy->getElementType(); |
| 2698 | DestTy = DestVecTy->getElementType(); |
| 2699 | } |
| 2700 | |
| 2701 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2702 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2703 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2704 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2705 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2706 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2707 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2708 | if (DestBits < SrcBits) |
| 2709 | return Trunc; // int -> smaller int |
| 2710 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2711 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2712 | return SExt; // signed -> SEXT |
| 2713 | else |
| 2714 | return ZExt; // unsigned -> ZEXT |
| 2715 | } else { |
| 2716 | return BitCast; // Same size, No-op cast |
| 2717 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2718 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2719 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2720 | return FPToSI; // FP -> sint |
| 2721 | else |
| 2722 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2723 | } else if (SrcTy->isVectorTy()) { |
| 2724 | assert(DestBits == SrcBits && |
| 2725 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2726 | return BitCast; // Same size, no-op cast |
| 2727 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2728 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2729 | "Casting from a value that is not first-class type"); |
| 2730 | return PtrToInt; // ptr -> int |
| 2731 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2732 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2733 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2734 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2735 | return SIToFP; // sint -> FP |
| 2736 | else |
| 2737 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2738 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2739 | if (DestBits < SrcBits) { |
| 2740 | return FPTrunc; // FP -> smaller FP |
| 2741 | } else if (DestBits > SrcBits) { |
| 2742 | return FPExt; // FP -> larger FP |
| 2743 | } else { |
| 2744 | return BitCast; // same size, no-op cast |
| 2745 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2746 | } else if (SrcTy->isVectorTy()) { |
| 2747 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2748 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2749 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2750 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2751 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2752 | } else if (DestTy->isVectorTy()) { |
| 2753 | assert(DestBits == SrcBits && |
| 2754 | "Illegal cast to vector (wrong type or size)"); |
| 2755 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2756 | } else if (DestTy->isPointerTy()) { |
| 2757 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2758 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 2759 | return AddrSpaceCast; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2760 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2761 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2762 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2763 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2764 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2765 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2766 | if (SrcTy->isVectorTy()) { |
| 2767 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2768 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2769 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2770 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2771 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2772 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | //===----------------------------------------------------------------------===// |
| 2776 | // CastInst SubClass Constructors |
| 2777 | //===----------------------------------------------------------------------===// |
| 2778 | |
| 2779 | /// Check that the construction parameters for a CastInst are correct. This |
| 2780 | /// could be broken out into the separate constructors but it is useful to have |
| 2781 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2782 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2783 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2784 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2785 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2786 | Type *SrcTy = S->getType(); |
Evan Cheng | 098d7b7 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 2787 | |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 2788 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 2789 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2790 | return false; |
| 2791 | |
| 2792 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2793 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2794 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2795 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2796 | // If these are vector types, get the lengths of the vectors (using zero for |
| 2797 | // scalar types means that checking that vector lengths match also checks that |
| 2798 | // scalars are not being converted to vectors or vectors to scalars). |
| 2799 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 2800 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 2801 | unsigned DstLength = DstTy->isVectorTy() ? |
| 2802 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 2803 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2804 | // Switch on the opcode provided |
| 2805 | switch (op) { |
| 2806 | default: return false; // This is an input error |
| 2807 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2808 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2809 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2810 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2811 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2812 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2813 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2814 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2815 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2816 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2817 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2818 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2819 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2820 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2821 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2822 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2823 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2824 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2825 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2826 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2827 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2828 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2829 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2830 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2831 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2832 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2833 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2834 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2835 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 2836 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2837 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2838 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2839 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2840 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2841 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2842 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 2843 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 2844 | case Instruction::BitCast: { |
| 2845 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 2846 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 2847 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2848 | // BitCast implies a no-op cast of type only. No bits change. |
| 2849 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 2850 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2851 | return false; |
| 2852 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2853 | // For non-pointer cases, the cast is okay if the source and destination bit |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2854 | // widths are identical. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 2855 | if (!SrcPtrTy) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2856 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 2857 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 2858 | // If both are pointers then the address spaces must match. |
| 2859 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 2860 | return false; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2861 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 2862 | // A vector of pointers must have the same number of elements. |
| 2863 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2864 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 2865 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 2866 | |
| 2867 | return false; |
| 2868 | } |
| 2869 | |
| 2870 | return true; |
| 2871 | } |
| 2872 | case Instruction::AddrSpaceCast: { |
| 2873 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 2874 | if (!SrcPtrTy) |
| 2875 | return false; |
| 2876 | |
| 2877 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 2878 | if (!DstPtrTy) |
| 2879 | return false; |
| 2880 | |
| 2881 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 2882 | return false; |
| 2883 | |
| 2884 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2885 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 2886 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 2887 | |
| 2888 | return false; |
| 2889 | } |
| 2890 | |
| 2891 | return true; |
| 2892 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2897 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2898 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2899 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2903 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2904 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2905 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2909 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2910 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2911 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2915 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2916 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2917 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2918 | } |
| 2919 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2920 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2921 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2922 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2925 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2926 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2927 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2928 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2932 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2933 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2934 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2938 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2939 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2940 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2944 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2945 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2946 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2950 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2951 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2952 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2956 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2957 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2958 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2962 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2963 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2964 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2968 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2969 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2970 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2974 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2975 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2976 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2980 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2981 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2982 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2986 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2987 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2988 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2992 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2993 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2994 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2998 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2999 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3000 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3004 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3005 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3006 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3010 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3011 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3012 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3016 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3017 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3018 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3022 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3023 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3024 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3028 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3029 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3030 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3034 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3035 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3036 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3037 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3038 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3039 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3040 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3041 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3042 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3043 | } |
| 3044 | |
| 3045 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3046 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3047 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3048 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3049 | } |
| 3050 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3051 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3052 | // CmpInst Classes |
| 3053 | //===----------------------------------------------------------------------===// |
| 3054 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3055 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3056 | Value *RHS, const Twine &Name, Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3057 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3058 | OperandTraits<CmpInst>::op_begin(this), |
| 3059 | OperandTraits<CmpInst>::operands(this), |
| 3060 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3061 | Op<0>() = LHS; |
| 3062 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3063 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3064 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3065 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3066 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3067 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3068 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3069 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3070 | OperandTraits<CmpInst>::op_begin(this), |
| 3071 | OperandTraits<CmpInst>::operands(this), |
| 3072 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3073 | Op<0>() = LHS; |
| 3074 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3075 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3076 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3080 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3081 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3082 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3083 | if (InsertBefore) |
| 3084 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3085 | S1, S2, Name); |
| 3086 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3087 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3088 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3089 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3090 | |
| 3091 | if (InsertBefore) |
| 3092 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3093 | S1, S2, Name); |
| 3094 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3095 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3096 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3100 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3101 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3102 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3103 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3104 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3105 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3106 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3107 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | void CmpInst::swapOperands() { |
| 3111 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3112 | IC->swapOperands(); |
| 3113 | else |
| 3114 | cast<FCmpInst>(this)->swapOperands(); |
| 3115 | } |
| 3116 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3117 | bool CmpInst::isCommutative() const { |
| 3118 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3119 | return IC->isCommutative(); |
| 3120 | return cast<FCmpInst>(this)->isCommutative(); |
| 3121 | } |
| 3122 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3123 | bool CmpInst::isEquality() const { |
| 3124 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3125 | return IC->isEquality(); |
| 3126 | return cast<FCmpInst>(this)->isEquality(); |
| 3127 | } |
| 3128 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3129 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3130 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3131 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3132 | case ICMP_EQ: return ICMP_NE; |
| 3133 | case ICMP_NE: return ICMP_EQ; |
| 3134 | case ICMP_UGT: return ICMP_ULE; |
| 3135 | case ICMP_ULT: return ICMP_UGE; |
| 3136 | case ICMP_UGE: return ICMP_ULT; |
| 3137 | case ICMP_ULE: return ICMP_UGT; |
| 3138 | case ICMP_SGT: return ICMP_SLE; |
| 3139 | case ICMP_SLT: return ICMP_SGE; |
| 3140 | case ICMP_SGE: return ICMP_SLT; |
| 3141 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3142 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3143 | case FCMP_OEQ: return FCMP_UNE; |
| 3144 | case FCMP_ONE: return FCMP_UEQ; |
| 3145 | case FCMP_OGT: return FCMP_ULE; |
| 3146 | case FCMP_OLT: return FCMP_UGE; |
| 3147 | case FCMP_OGE: return FCMP_ULT; |
| 3148 | case FCMP_OLE: return FCMP_UGT; |
| 3149 | case FCMP_UEQ: return FCMP_ONE; |
| 3150 | case FCMP_UNE: return FCMP_OEQ; |
| 3151 | case FCMP_UGT: return FCMP_OLE; |
| 3152 | case FCMP_ULT: return FCMP_OGE; |
| 3153 | case FCMP_UGE: return FCMP_OLT; |
| 3154 | case FCMP_ULE: return FCMP_OGT; |
| 3155 | case FCMP_ORD: return FCMP_UNO; |
| 3156 | case FCMP_UNO: return FCMP_ORD; |
| 3157 | case FCMP_TRUE: return FCMP_FALSE; |
| 3158 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
| 3161 | |
Tim Northover | de3aea041 | 2016-08-17 20:25:25 +0000 | [diff] [blame] | 3162 | StringRef CmpInst::getPredicateName(Predicate Pred) { |
| 3163 | switch (Pred) { |
| 3164 | default: return "unknown"; |
| 3165 | case FCmpInst::FCMP_FALSE: return "false"; |
| 3166 | case FCmpInst::FCMP_OEQ: return "oeq"; |
| 3167 | case FCmpInst::FCMP_OGT: return "ogt"; |
| 3168 | case FCmpInst::FCMP_OGE: return "oge"; |
| 3169 | case FCmpInst::FCMP_OLT: return "olt"; |
| 3170 | case FCmpInst::FCMP_OLE: return "ole"; |
| 3171 | case FCmpInst::FCMP_ONE: return "one"; |
| 3172 | case FCmpInst::FCMP_ORD: return "ord"; |
| 3173 | case FCmpInst::FCMP_UNO: return "uno"; |
| 3174 | case FCmpInst::FCMP_UEQ: return "ueq"; |
| 3175 | case FCmpInst::FCMP_UGT: return "ugt"; |
| 3176 | case FCmpInst::FCMP_UGE: return "uge"; |
| 3177 | case FCmpInst::FCMP_ULT: return "ult"; |
| 3178 | case FCmpInst::FCMP_ULE: return "ule"; |
| 3179 | case FCmpInst::FCMP_UNE: return "une"; |
| 3180 | case FCmpInst::FCMP_TRUE: return "true"; |
| 3181 | case ICmpInst::ICMP_EQ: return "eq"; |
| 3182 | case ICmpInst::ICMP_NE: return "ne"; |
| 3183 | case ICmpInst::ICMP_SGT: return "sgt"; |
| 3184 | case ICmpInst::ICMP_SGE: return "sge"; |
| 3185 | case ICmpInst::ICMP_SLT: return "slt"; |
| 3186 | case ICmpInst::ICMP_SLE: return "sle"; |
| 3187 | case ICmpInst::ICMP_UGT: return "ugt"; |
| 3188 | case ICmpInst::ICMP_UGE: return "uge"; |
| 3189 | case ICmpInst::ICMP_ULT: return "ult"; |
| 3190 | case ICmpInst::ICMP_ULE: return "ule"; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3194 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3195 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3196 | default: llvm_unreachable("Unknown icmp predicate!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3197 | case ICMP_EQ: case ICMP_NE: |
| 3198 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 3199 | return pred; |
| 3200 | case ICMP_UGT: return ICMP_SGT; |
| 3201 | case ICMP_ULT: return ICMP_SLT; |
| 3202 | case ICMP_UGE: return ICMP_SGE; |
| 3203 | case ICMP_ULE: return ICMP_SLE; |
| 3204 | } |
| 3205 | } |
| 3206 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3207 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3208 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3209 | default: llvm_unreachable("Unknown icmp predicate!"); |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3210 | case ICMP_EQ: case ICMP_NE: |
| 3211 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 3212 | return pred; |
| 3213 | case ICMP_SGT: return ICMP_UGT; |
| 3214 | case ICMP_SLT: return ICMP_ULT; |
| 3215 | case ICMP_SGE: return ICMP_UGE; |
| 3216 | case ICMP_SLE: return ICMP_ULE; |
| 3217 | } |
| 3218 | } |
| 3219 | |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 3220 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { |
| 3221 | switch (pred) { |
| 3222 | default: llvm_unreachable("Unknown or unsupported cmp predicate!"); |
| 3223 | case ICMP_SGT: return ICMP_SGE; |
| 3224 | case ICMP_SLT: return ICMP_SLE; |
| 3225 | case ICMP_SGE: return ICMP_SGT; |
| 3226 | case ICMP_SLE: return ICMP_SLT; |
| 3227 | case ICMP_UGT: return ICMP_UGE; |
| 3228 | case ICMP_ULT: return ICMP_ULE; |
| 3229 | case ICMP_UGE: return ICMP_UGT; |
| 3230 | case ICMP_ULE: return ICMP_ULT; |
| 3231 | |
| 3232 | case FCMP_OGT: return FCMP_OGE; |
| 3233 | case FCMP_OLT: return FCMP_OLE; |
| 3234 | case FCMP_OGE: return FCMP_OGT; |
| 3235 | case FCMP_OLE: return FCMP_OLT; |
| 3236 | case FCMP_UGT: return FCMP_UGE; |
| 3237 | case FCMP_ULT: return FCMP_ULE; |
| 3238 | case FCMP_UGE: return FCMP_UGT; |
| 3239 | case FCMP_ULE: return FCMP_ULT; |
| 3240 | } |
| 3241 | } |
| 3242 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3243 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3244 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3245 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3246 | case ICMP_EQ: case ICMP_NE: |
| 3247 | return pred; |
| 3248 | case ICMP_SGT: return ICMP_SLT; |
| 3249 | case ICMP_SLT: return ICMP_SGT; |
| 3250 | case ICMP_SGE: return ICMP_SLE; |
| 3251 | case ICMP_SLE: return ICMP_SGE; |
| 3252 | case ICMP_UGT: return ICMP_ULT; |
| 3253 | case ICMP_ULT: return ICMP_UGT; |
| 3254 | case ICMP_UGE: return ICMP_ULE; |
| 3255 | case ICMP_ULE: return ICMP_UGE; |
| 3256 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3257 | case FCMP_FALSE: case FCMP_TRUE: |
| 3258 | case FCMP_OEQ: case FCMP_ONE: |
| 3259 | case FCMP_UEQ: case FCMP_UNE: |
| 3260 | case FCMP_ORD: case FCMP_UNO: |
| 3261 | return pred; |
| 3262 | case FCMP_OGT: return FCMP_OLT; |
| 3263 | case FCMP_OLT: return FCMP_OGT; |
| 3264 | case FCMP_OGE: return FCMP_OLE; |
| 3265 | case FCMP_OLE: return FCMP_OGE; |
| 3266 | case FCMP_UGT: return FCMP_ULT; |
| 3267 | case FCMP_ULT: return FCMP_UGT; |
| 3268 | case FCMP_UGE: return FCMP_ULE; |
| 3269 | case FCMP_ULE: return FCMP_UGE; |
| 3270 | } |
| 3271 | } |
| 3272 | |
Max Kazantsev | b299ade | 2018-02-07 11:16:29 +0000 | [diff] [blame] | 3273 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { |
| 3274 | switch (pred) { |
| 3275 | case ICMP_SGT: return ICMP_SGE; |
| 3276 | case ICMP_SLT: return ICMP_SLE; |
| 3277 | case ICMP_UGT: return ICMP_UGE; |
| 3278 | case ICMP_ULT: return ICMP_ULE; |
| 3279 | case FCMP_OGT: return FCMP_OGE; |
| 3280 | case FCMP_OLT: return FCMP_OLE; |
| 3281 | case FCMP_UGT: return FCMP_UGE; |
| 3282 | case FCMP_ULT: return FCMP_ULE; |
| 3283 | default: return pred; |
| 3284 | } |
| 3285 | } |
| 3286 | |
Sanjoy Das | 6e78b17 | 2015-10-22 19:57:34 +0000 | [diff] [blame] | 3287 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { |
| 3288 | assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!"); |
| 3289 | |
| 3290 | switch (pred) { |
| 3291 | default: |
| 3292 | llvm_unreachable("Unknown predicate!"); |
| 3293 | case CmpInst::ICMP_ULT: |
| 3294 | return CmpInst::ICMP_SLT; |
| 3295 | case CmpInst::ICMP_ULE: |
| 3296 | return CmpInst::ICMP_SLE; |
| 3297 | case CmpInst::ICMP_UGT: |
| 3298 | return CmpInst::ICMP_SGT; |
| 3299 | case CmpInst::ICMP_UGE: |
| 3300 | return CmpInst::ICMP_SGE; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3304 | bool CmpInst::isUnsigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3305 | switch (predicate) { |
| 3306 | default: return false; |
| 3307 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 3308 | case ICmpInst::ICMP_UGE: return true; |
| 3309 | } |
| 3310 | } |
| 3311 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3312 | bool CmpInst::isSigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3313 | switch (predicate) { |
| 3314 | default: return false; |
| 3315 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 3316 | case ICmpInst::ICMP_SGE: return true; |
| 3317 | } |
| 3318 | } |
| 3319 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3320 | bool CmpInst::isOrdered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3321 | switch (predicate) { |
| 3322 | default: return false; |
| 3323 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3324 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 3325 | case FCmpInst::FCMP_ORD: return true; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3329 | bool CmpInst::isUnordered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3330 | switch (predicate) { |
| 3331 | default: return false; |
| 3332 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3333 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 3334 | case FCmpInst::FCMP_UNO: return true; |
| 3335 | } |
| 3336 | } |
| 3337 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3338 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3339 | switch(predicate) { |
| 3340 | default: return false; |
| 3341 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3342 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3343 | } |
| 3344 | } |
| 3345 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3346 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3347 | switch(predicate) { |
| 3348 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3349 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3350 | default: return false; |
| 3351 | } |
| 3352 | } |
| 3353 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3354 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3355 | // If the predicates match, then we know the first condition implies the |
| 3356 | // second is true. |
| 3357 | if (Pred1 == Pred2) |
| 3358 | return true; |
| 3359 | |
| 3360 | switch (Pred1) { |
| 3361 | default: |
| 3362 | break; |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3363 | case ICMP_EQ: |
Chad Rosier | 3d75f8c | 2016-04-25 13:25:14 +0000 | [diff] [blame] | 3364 | // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3365 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || |
| 3366 | Pred2 == ICMP_SLE; |
Chad Rosier | 3456cb5 | 2016-04-22 17:14:12 +0000 | [diff] [blame] | 3367 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. |
| 3368 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; |
| 3369 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. |
| 3370 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; |
| 3371 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. |
| 3372 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; |
| 3373 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. |
| 3374 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3375 | } |
| 3376 | return false; |
| 3377 | } |
| 3378 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3379 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3380 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3381 | } |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3382 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3383 | //===----------------------------------------------------------------------===// |
| 3384 | // SwitchInst Implementation |
| 3385 | //===----------------------------------------------------------------------===// |
| 3386 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3387 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3388 | assert(Value && Default && NumReserved); |
| 3389 | ReservedSpace = NumReserved; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3390 | setNumHungOffUseOperands(2); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3391 | allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3392 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3393 | Op<0>() = Value; |
| 3394 | Op<1>() = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3397 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3398 | /// switch on and a default destination. The number of additional cases can |
| 3399 | /// be specified here to make memory allocation more efficient. This |
| 3400 | /// constructor can also autoinsert before another instruction. |
| 3401 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3402 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3403 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3404 | nullptr, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3405 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3409 | /// switch on and a default destination. The number of additional cases can |
| 3410 | /// be specified here to make memory allocation more efficient. This |
| 3411 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3412 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3413 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3414 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3415 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3416 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3419 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3420 | : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3421 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3422 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3423 | Use *OL = getOperandList(); |
| 3424 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3425 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3426 | OL[i] = InOL[i]; |
| 3427 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3428 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3429 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3432 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3433 | /// addCase - Add an entry to the switch instruction... |
| 3434 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3435 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3436 | unsigned NewCaseIdx = getNumCases(); |
| 3437 | unsigned OpNo = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3438 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3439 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3440 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3441 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3442 | setNumHungOffUseOperands(OpNo+2); |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3443 | CaseHandle Case(this, NewCaseIdx); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3444 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3445 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3448 | /// removeCase - This method removes the specified case and its successor |
| 3449 | /// from the switch instruction. |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3450 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { |
| 3451 | unsigned idx = I->getCaseIndex(); |
| 3452 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3453 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3454 | |
| 3455 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3456 | Use *OL = getOperandList(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3457 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3458 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3459 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3460 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3461 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3465 | OL[NumOps-2].set(nullptr); |
| 3466 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3467 | setNumHungOffUseOperands(NumOps-2); |
Chandler Carruth | 0d256c0 | 2017-03-26 02:49:23 +0000 | [diff] [blame] | 3468 | |
| 3469 | return CaseIt(this, idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3472 | /// growOperands - grow operands - This grows the operand list in response |
| 3473 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3474 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3475 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3476 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3477 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3478 | |
| 3479 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3480 | growHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3483 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3484 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3485 | //===----------------------------------------------------------------------===// |
| 3486 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3487 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3488 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3489 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3490 | ReservedSpace = 1+NumDests; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3491 | setNumHungOffUseOperands(1); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3492 | allocHungoffUses(ReservedSpace); |
| 3493 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3494 | Op<0>() = Address; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3498 | /// growOperands - grow operands - This grows the operand list in response |
| 3499 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3500 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3501 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3502 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3503 | unsigned NumOps = e*2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3504 | |
| 3505 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3506 | growHungoffUses(ReservedSpace); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3509 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3510 | Instruction *InsertBefore) |
| 3511 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3512 | nullptr, 0, InsertBefore) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3513 | init(Address, NumCases); |
| 3514 | } |
| 3515 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3516 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3517 | BasicBlock *InsertAtEnd) |
| 3518 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3519 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3520 | init(Address, NumCases); |
| 3521 | } |
| 3522 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3523 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3524 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3525 | nullptr, IBI.getNumOperands()) { |
| 3526 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3527 | Use *OL = getOperandList(); |
| 3528 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3529 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3530 | OL[i] = InOL[i]; |
| 3531 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3532 | } |
| 3533 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3534 | /// addDestination - Add a destination. |
| 3535 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3536 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3537 | unsigned OpNo = getNumOperands(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3538 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3539 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3540 | // Initialize some new operands. |
| 3541 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3542 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3543 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3547 | /// indirectbr instruction. |
| 3548 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3549 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3550 | |
| 3551 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3552 | Use *OL = getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3553 | |
| 3554 | // Replace this value with the last one. |
| 3555 | OL[idx+1] = OL[NumOps-1]; |
| 3556 | |
| 3557 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3558 | OL[NumOps-1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3559 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3562 | //===----------------------------------------------------------------------===// |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3563 | // cloneImpl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3564 | //===----------------------------------------------------------------------===// |
| 3565 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3566 | // Define these methods here so vtables don't get emitted into every translation |
| 3567 | // unit that uses these classes. |
| 3568 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3569 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3570 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3573 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3574 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3577 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3578 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3581 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3582 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3585 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3586 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3589 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3590 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3593 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3594 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 3595 | getType()->getAddressSpace(), |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3596 | (Value *)getOperand(0), getAlignment()); |
| 3597 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 3598 | Result->setSwiftError(isSwiftError()); |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3599 | return Result; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3602 | LoadInst *LoadInst::cloneImpl() const { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3603 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3604 | getAlignment(), getOrdering(), getSyncScopeID()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3607 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3608 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3609 | getAlignment(), getOrdering(), getSyncScopeID()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3610 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3613 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3614 | AtomicCmpXchgInst *Result = |
| 3615 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3616 | getSuccessOrdering(), getFailureOrdering(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3617 | getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3618 | Result->setVolatile(isVolatile()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3619 | Result->setWeak(isWeak()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3620 | return Result; |
| 3621 | } |
| 3622 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3623 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3624 | AtomicRMWInst *Result = |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3625 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), |
| 3626 | getOrdering(), getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3627 | Result->setVolatile(isVolatile()); |
| 3628 | return Result; |
| 3629 | } |
| 3630 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3631 | FenceInst *FenceInst::cloneImpl() const { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3632 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3635 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3636 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3639 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3640 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3643 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3644 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3647 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3648 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3651 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3652 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3655 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3656 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3659 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3660 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3663 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3664 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3667 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3668 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3671 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3672 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3675 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3676 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3677 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3678 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3679 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3680 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3681 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3682 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3683 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3684 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 3685 | } |
| 3686 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3687 | CallInst *CallInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3688 | if (hasOperandBundles()) { |
| 3689 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3690 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); |
| 3691 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3692 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3695 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3696 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3697 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3698 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3699 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3700 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3701 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3702 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3703 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3704 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3705 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3706 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3707 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3708 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3711 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3712 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3713 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3714 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3715 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3716 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3717 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3718 | return new LandingPadInst(*this); |
| 3719 | } |
| 3720 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3721 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3722 | return new(getNumOperands()) ReturnInst(*this); |
| 3723 | } |
| 3724 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3725 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3726 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3727 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3728 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3729 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3730 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3731 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3732 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3735 | InvokeInst *InvokeInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3736 | if (hasOperandBundles()) { |
| 3737 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3738 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); |
| 3739 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3740 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3741 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3742 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3743 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3744 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3745 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 3746 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 3747 | } |
| 3748 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3749 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3750 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3753 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { |
| 3754 | return new CatchSwitchInst(*this); |
| 3755 | } |
| 3756 | |
| 3757 | FuncletPadInst *FuncletPadInst::cloneImpl() const { |
| 3758 | return new (getNumOperands()) FuncletPadInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3761 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3762 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3763 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3764 | } |