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