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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 9 | // This file implements all of the non-inline methods for the LLVM instruction |
| 10 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Instructions.h" |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/IR/Attributes.h" |
| 20 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 27 | #include "llvm/IR/InstrTypes.h" |
| 28 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Intrinsics.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
Wei Mi | 01f8d55 | 2019-04-22 17:04:51 +0000 | [diff] [blame] | 31 | #include "llvm/IR/MDBuilder.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Type.h" |
| 36 | #include "llvm/IR/Value.h" |
| 37 | #include "llvm/Support/AtomicOrdering.h" |
| 38 | #include "llvm/Support/Casting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <cstdint> |
| 44 | #include <vector> |
| 45 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
Bjorn Pettersson | 550517b | 2018-06-26 06:17:00 +0000 | [diff] [blame] | 49 | // AllocaInst Class |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | Optional<uint64_t> |
| 53 | AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { |
| 54 | uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType()); |
| 55 | if (isArrayAllocation()) { |
| 56 | auto C = dyn_cast<ConstantInt>(getArraySize()); |
| 57 | if (!C) |
| 58 | return None; |
| 59 | Size *= C->getZExtValue(); |
| 60 | } |
| 61 | return Size; |
| 62 | } |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 65 | // CallSite Class |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 68 | User::op_iterator CallSite::getCallee() const { |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 69 | return cast<CallBase>(getInstruction())->op_end() - 1; |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 73 | // SelectInst Class |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
| 76 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 77 | /// for a select operation, otherwise return null. |
| 78 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 79 | if (Op1->getType() != Op2->getType()) |
| 80 | return "both values to select must have same type"; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 81 | |
| 82 | if (Op1->getType()->isTokenTy()) |
| 83 | return "select values cannot have token type"; |
| 84 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 85 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 86 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 87 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 88 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 89 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 90 | if (!ET) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 91 | return "selected values for vector select must be vectors"; |
| 92 | if (ET->getNumElements() != VT->getNumElements()) |
| 93 | return "vector select requires selected vectors to have " |
| 94 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 95 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 96 | return "select condition must be i1 or <n x i1>"; |
| 97 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 98 | return nullptr; |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 102 | // PHINode Class |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 106 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 107 | ReservedSpace(PN.getNumOperands()) { |
| 108 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 109 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 110 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 111 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 114 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 115 | // predecessor basic block is deleted. |
| 116 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 117 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 118 | |
| 119 | // Move everything after this operand down. |
| 120 | // |
| 121 | // 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] | 122 | // 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] | 123 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 124 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 125 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 126 | |
| 127 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 128 | Op<-1>().set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 129 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 130 | |
| 131 | // 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] | 132 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 133 | // 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] | 134 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 135 | eraseFromParent(); |
| 136 | } |
| 137 | return Removed; |
| 138 | } |
| 139 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 140 | /// growOperands - grow operands - This grows the operand list in response |
| 141 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 142 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 143 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 144 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 145 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 146 | unsigned NumOps = e + e / 2; |
| 147 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 148 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 150 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 153 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 154 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 155 | Value *PHINode::hasConstantValue() const { |
| 156 | // Exploit the fact that phi nodes always have at least one entry. |
| 157 | Value *ConstantValue = getIncomingValue(0); |
| 158 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 159 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 160 | if (ConstantValue != this) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 161 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 162 | // The case where the first value is this PHI. |
| 163 | ConstantValue = getIncomingValue(i); |
| 164 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 165 | if (ConstantValue == this) |
| 166 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 167 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Nicolai Haehnle | 13d90f3 | 2016-04-14 17:42:47 +0000 | [diff] [blame] | 170 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges |
| 171 | /// together the same value, assuming that undefs result in the same value as |
| 172 | /// non-undefs. |
| 173 | /// Unlike \ref hasConstantValue, this does not return a value because the |
| 174 | /// unique non-undef incoming value need not dominate the PHI node. |
| 175 | bool PHINode::hasConstantOrUndefValue() const { |
| 176 | Value *ConstantValue = nullptr; |
| 177 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { |
| 178 | Value *Incoming = getIncomingValue(i); |
| 179 | if (Incoming != this && !isa<UndefValue>(Incoming)) { |
| 180 | if (ConstantValue && ConstantValue != Incoming) |
| 181 | return false; |
| 182 | ConstantValue = Incoming; |
| 183 | } |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // LandingPadInst Implementation |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 192 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 193 | const Twine &NameStr, Instruction *InsertBefore) |
| 194 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 195 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 198 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 199 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 200 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 201 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 205 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 206 | LP.getNumOperands()), |
| 207 | ReservedSpace(LP.getNumOperands()) { |
| 208 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 209 | Use *OL = getOperandList(); |
| 210 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 211 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 212 | OL[I] = InOL[I]; |
| 213 | |
| 214 | setCleanup(LP.isCleanup()); |
| 215 | } |
| 216 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 217 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 218 | const Twine &NameStr, |
| 219 | Instruction *InsertBefore) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 220 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 221 | } |
| 222 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 223 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 224 | const Twine &NameStr, |
| 225 | BasicBlock *InsertAtEnd) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 226 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 227 | } |
| 228 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 229 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 230 | ReservedSpace = NumReservedValues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 231 | setNumHungOffUseOperands(0); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 232 | allocHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 233 | setName(NameStr); |
| 234 | setCleanup(false); |
| 235 | } |
| 236 | |
| 237 | /// growOperands - grow operands - This grows the operand list in response to a |
| 238 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 239 | void LandingPadInst::growOperands(unsigned Size) { |
| 240 | unsigned e = getNumOperands(); |
| 241 | if (ReservedSpace >= e + Size) return; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 242 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 243 | growHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 246 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 247 | unsigned OpNo = getNumOperands(); |
| 248 | growOperands(1); |
| 249 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 250 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 251 | getOperandList()[OpNo] = Val; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 253 | |
| 254 | //===----------------------------------------------------------------------===// |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 255 | // CallBase Implementation |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 258 | Function *CallBase::getCaller() { return getParent()->getParent(); } |
| 259 | |
Craig Topper | 784929d | 2019-02-08 20:48:56 +0000 | [diff] [blame] | 260 | unsigned CallBase::getNumSubclassExtraOperandsDynamic() const { |
| 261 | assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!"); |
| 262 | return cast<CallBrInst>(this)->getNumIndirectDests() + 1; |
| 263 | } |
| 264 | |
Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 265 | bool CallBase::isIndirectCall() const { |
| 266 | const Value *V = getCalledValue(); |
| 267 | if (isa<Function>(V) || isa<Constant>(V)) |
| 268 | return false; |
| 269 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 270 | if (CI->isInlineAsm()) |
| 271 | return false; |
| 272 | return true; |
| 273 | } |
| 274 | |
Craig Topper | c1892ec | 2019-01-31 17:23:29 +0000 | [diff] [blame] | 275 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
| 276 | /// be tail call optimized. |
| 277 | bool CallBase::isMustTailCall() const { |
| 278 | if (auto *CI = dyn_cast<CallInst>(this)) |
| 279 | return CI->isMustTailCall(); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /// Tests if this call site is marked as a tail call. |
| 284 | bool CallBase::isTailCall() const { |
| 285 | if (auto *CI = dyn_cast<CallInst>(this)) |
| 286 | return CI->isTailCall(); |
| 287 | return false; |
| 288 | } |
| 289 | |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 290 | Intrinsic::ID CallBase::getIntrinsicID() const { |
| 291 | if (auto *F = getCalledFunction()) |
| 292 | return F->getIntrinsicID(); |
| 293 | return Intrinsic::not_intrinsic; |
| 294 | } |
| 295 | |
| 296 | bool CallBase::isReturnNonNull() const { |
| 297 | if (hasRetAttr(Attribute::NonNull)) |
| 298 | return true; |
| 299 | |
| 300 | if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 && |
| 301 | !NullPointerIsDefined(getCaller(), |
| 302 | getType()->getPointerAddressSpace())) |
| 303 | return true; |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 308 | Value *CallBase::getReturnedArgOperand() const { |
| 309 | unsigned Index; |
| 310 | |
| 311 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) |
| 312 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 313 | if (const Function *F = getCalledFunction()) |
| 314 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && |
| 315 | Index) |
| 316 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 317 | |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const { |
| 322 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
| 323 | return true; |
| 324 | |
| 325 | // Look at the callee, if available. |
| 326 | if (const Function *F = getCalledFunction()) |
| 327 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | /// Determine whether the argument or parameter has the given attribute. |
| 332 | bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
| 333 | assert(ArgNo < getNumArgOperands() && "Param index out of bounds!"); |
| 334 | |
| 335 | if (Attrs.hasParamAttribute(ArgNo, Kind)) |
| 336 | return true; |
| 337 | if (const Function *F = getCalledFunction()) |
| 338 | return F->getAttributes().hasParamAttribute(ArgNo, Kind); |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { |
| 343 | if (const Function *F = getCalledFunction()) |
| 344 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { |
| 349 | if (const Function *F = getCalledFunction()) |
| 350 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | CallBase::op_iterator |
| 355 | CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
| 356 | const unsigned BeginIndex) { |
| 357 | auto It = op_begin() + BeginIndex; |
| 358 | for (auto &B : Bundles) |
| 359 | It = std::copy(B.input_begin(), B.input_end(), It); |
| 360 | |
| 361 | auto *ContextImpl = getContext().pImpl; |
| 362 | auto BI = Bundles.begin(); |
| 363 | unsigned CurrentIndex = BeginIndex; |
| 364 | |
| 365 | for (auto &BOI : bundle_op_infos()) { |
| 366 | assert(BI != Bundles.end() && "Incorrect allocation?"); |
| 367 | |
| 368 | BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); |
| 369 | BOI.Begin = CurrentIndex; |
| 370 | BOI.End = CurrentIndex + BI->input_size(); |
| 371 | CurrentIndex = BOI.End; |
| 372 | BI++; |
| 373 | } |
| 374 | |
| 375 | assert(BI == Bundles.end() && "Incorrect allocation?"); |
| 376 | |
| 377 | return It; |
| 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 381 | // CallInst Implementation |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 384 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 385 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 386 | this->FTy = FTy; |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 387 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 388 | "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 389 | setCalledOperand(Func); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 390 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 391 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 392 | assert((Args.size() == FTy->getNumParams() || |
| 393 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 394 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 395 | |
| 396 | for (unsigned i = 0; i != Args.size(); ++i) |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 397 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 398 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 399 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 400 | #endif |
| 401 | |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 402 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 403 | |
| 404 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 405 | (void)It; |
| 406 | assert(It + 1 == op_end() && "Should add up!"); |
| 407 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 408 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 409 | } |
| 410 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 411 | void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { |
| 412 | this->FTy = FTy; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 413 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 414 | setCalledOperand(Func); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 415 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 416 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 417 | |
| 418 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 419 | } |
| 420 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 421 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 422 | Instruction *InsertBefore) |
| 423 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 424 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { |
| 425 | init(Ty, Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 428 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 429 | BasicBlock *InsertAtEnd) |
| 430 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 431 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { |
| 432 | init(Ty, Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 435 | CallInst::CallInst(const CallInst &CI) |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 436 | : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, |
| 437 | OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), |
| 438 | CI.getNumOperands()) { |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 439 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 440 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 441 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 442 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 443 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 444 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 445 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 448 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, |
| 449 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 450 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 451 | |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 452 | auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledValue(), |
| 453 | Args, OpB, CI->getName(), InsertPt); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 454 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 455 | NewCI->setCallingConv(CI->getCallingConv()); |
| 456 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 457 | NewCI->setAttributes(CI->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 458 | NewCI->setDebugLoc(CI->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 459 | return NewCI; |
| 460 | } |
| 461 | |
Wei Mi | 01f8d55 | 2019-04-22 17:04:51 +0000 | [diff] [blame] | 462 | // Update profile weight for call instruction by scaling it using the ratio |
| 463 | // of S/T. The meaning of "branch_weights" meta data for call instruction is |
| 464 | // transfered to represent call count. |
| 465 | void CallInst::updateProfWeight(uint64_t S, uint64_t T) { |
| 466 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 467 | if (ProfileData == nullptr) |
| 468 | return; |
| 469 | |
| 470 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); |
| 471 | if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && |
| 472 | !ProfDataName->getString().equals("VP"))) |
| 473 | return; |
| 474 | |
| 475 | MDBuilder MDB(getContext()); |
| 476 | SmallVector<Metadata *, 3> Vals; |
| 477 | Vals.push_back(ProfileData->getOperand(0)); |
| 478 | APInt APS(128, S), APT(128, T); |
| 479 | if (ProfDataName->getString().equals("branch_weights") && |
| 480 | ProfileData->getNumOperands() > 0) { |
| 481 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 482 | APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)) |
| 483 | ->getValue() |
| 484 | .getZExtValue()); |
| 485 | Val *= APS; |
| 486 | Vals.push_back(MDB.createConstant(ConstantInt::get( |
| 487 | Type::getInt64Ty(getContext()), Val.udiv(APT).getLimitedValue()))); |
| 488 | } else if (ProfDataName->getString().equals("VP")) |
| 489 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { |
| 490 | // The first value is the key of the value profile, which will not change. |
| 491 | Vals.push_back(ProfileData->getOperand(i)); |
| 492 | // Using APInt::div may be expensive, but most cases should fit 64 bits. |
| 493 | APInt Val(128, |
| 494 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) |
| 495 | ->getValue() |
| 496 | .getZExtValue()); |
| 497 | Val *= APS; |
| 498 | Vals.push_back(MDB.createConstant( |
| 499 | ConstantInt::get(Type::getInt64Ty(getContext()), |
| 500 | Val.udiv(APT).getLimitedValue()))); |
| 501 | } |
| 502 | setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); |
| 503 | } |
| 504 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 505 | /// IsConstantOne - Return true only if val is constant int 1 |
| 506 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 507 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 6941785 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 508 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 509 | return CVal && CVal->isOne(); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 512 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 513 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 514 | Type *AllocTy, Value *AllocSize, |
| 515 | Value *ArraySize, |
| 516 | ArrayRef<OperandBundleDef> OpB, |
| 517 | Function *MallocF, const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 518 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 519 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 520 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 521 | // malloc(type) becomes: |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 522 | // bitcast (i8* malloc(typeSize)) to type* |
| 523 | // malloc(type, arraySize) becomes: |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 524 | // bitcast (i8* malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 525 | if (!ArraySize) |
| 526 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 527 | else if (ArraySize->getType() != IntPtrTy) { |
| 528 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 529 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 530 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 531 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 532 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 533 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 534 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 535 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 536 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 537 | if (IsConstantOne(AllocSize)) { |
| 538 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 539 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 540 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 541 | false /*ZExt*/); |
| 542 | // Malloc arg is constant product of type size and array size |
| 543 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 544 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 545 | // Multiply type size by the array size... |
| 546 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 547 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 548 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 549 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 550 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 551 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 552 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 553 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 554 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 555 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 556 | // Create the call to Malloc. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 557 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 558 | Module *M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 559 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 560 | FunctionCallee MallocFunc = MallocF; |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 561 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 562 | // prototype malloc as "void *malloc(size_t)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 563 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 564 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 565 | CallInst *MCall = nullptr; |
| 566 | Instruction *Result = nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 567 | if (InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 568 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", |
| 569 | InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 570 | Result = MCall; |
| 571 | if (Result->getType() != AllocPtrType) |
| 572 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 573 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 574 | } else { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 575 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 576 | Result = MCall; |
| 577 | if (Result->getType() != AllocPtrType) { |
| 578 | InsertAtEnd->getInstList().push_back(MCall); |
| 579 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 580 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 581 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 582 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 583 | MCall->setTailCall(); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 584 | if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 585 | MCall->setCallingConv(F->getCallingConv()); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 586 | if (!F->returnDoesNotAlias()) |
| 587 | F->setReturnDoesNotAlias(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 588 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 589 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 590 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 591 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 595 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 596 | /// possibly multiplied by the array size if the array size is not |
| 597 | /// constant 1. |
| 598 | /// 2. Call malloc with that argument. |
| 599 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 600 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 601 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 602 | Value *AllocSize, Value *ArraySize, |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 603 | Function *MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 604 | const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 605 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 606 | ArraySize, None, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 607 | } |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 608 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 609 | Type *IntPtrTy, Type *AllocTy, |
| 610 | Value *AllocSize, Value *ArraySize, |
| 611 | ArrayRef<OperandBundleDef> OpB, |
| 612 | Function *MallocF, |
| 613 | const Twine &Name) { |
| 614 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
| 615 | ArraySize, OpB, MallocF, Name); |
| 616 | } |
| 617 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 618 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 619 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 620 | /// possibly multiplied by the array size if the array size is not |
| 621 | /// constant 1. |
| 622 | /// 2. Call malloc with that argument. |
| 623 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 624 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 625 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 626 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 627 | Type *IntPtrTy, Type *AllocTy, |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 628 | Value *AllocSize, Value *ArraySize, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 629 | Function *MallocF, const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 630 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 631 | ArraySize, None, MallocF, Name); |
| 632 | } |
| 633 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 634 | Type *IntPtrTy, Type *AllocTy, |
| 635 | Value *AllocSize, Value *ArraySize, |
| 636 | ArrayRef<OperandBundleDef> OpB, |
| 637 | Function *MallocF, const Twine &Name) { |
| 638 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
| 639 | ArraySize, OpB, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 640 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 641 | |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 642 | static Instruction *createFree(Value *Source, |
| 643 | ArrayRef<OperandBundleDef> Bundles, |
| 644 | Instruction *InsertBefore, |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 645 | BasicBlock *InsertAtEnd) { |
| 646 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 647 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 648 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 649 | "Can not free something of nonpointer type!"); |
| 650 | |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 651 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 652 | Module *M = BB->getParent()->getParent(); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 654 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 655 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 656 | // prototype free as "void free(void*)" |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 657 | FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 658 | CallInst *Result = nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 659 | Value *PtrCast = Source; |
| 660 | if (InsertBefore) { |
| 661 | if (Source->getType() != IntPtrTy) |
| 662 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 663 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 664 | } else { |
| 665 | if (Source->getType() != IntPtrTy) |
| 666 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 667 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 668 | } |
| 669 | Result->setTailCall(); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 670 | if (Function *F = dyn_cast<Function>(FreeFunc.getCallee())) |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 671 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 672 | |
| 673 | return Result; |
| 674 | } |
| 675 | |
| 676 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 677 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 678 | return createFree(Source, None, InsertBefore, nullptr); |
| 679 | } |
| 680 | Instruction *CallInst::CreateFree(Value *Source, |
| 681 | ArrayRef<OperandBundleDef> Bundles, |
| 682 | Instruction *InsertBefore) { |
| 683 | return createFree(Source, Bundles, InsertBefore, nullptr); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 687 | /// Note: This function does not add the call to the basic block, that is the |
| 688 | /// responsibility of the caller. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 689 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 690 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); |
| 691 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 692 | return FreeCall; |
| 693 | } |
| 694 | Instruction *CallInst::CreateFree(Value *Source, |
| 695 | ArrayRef<OperandBundleDef> Bundles, |
| 696 | BasicBlock *InsertAtEnd) { |
| 697 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 698 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 699 | return FreeCall; |
| 700 | } |
| 701 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 702 | //===----------------------------------------------------------------------===// |
| 703 | // InvokeInst Implementation |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 706 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 707 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 708 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 709 | const Twine &NameStr) { |
| 710 | this->FTy = FTy; |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 711 | |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 712 | assert((int)getNumOperands() == |
| 713 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) && |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 714 | "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 715 | setNormalDest(IfNormal); |
| 716 | setUnwindDest(IfException); |
| 717 | setCalledOperand(Fn); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 718 | |
| 719 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 720 | assert(((Args.size() == FTy->getNumParams()) || |
| 721 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 722 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 723 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 724 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 725 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 726 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 727 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 728 | #endif |
| 729 | |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 730 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 731 | |
| 732 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 733 | (void)It; |
| 734 | assert(It + 3 == op_end() && "Should add up!"); |
| 735 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 736 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 739 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 740 | : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, |
| 741 | OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), |
| 742 | II.getNumOperands()) { |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 743 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 744 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 745 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 746 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 747 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 750 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, |
| 751 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 752 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 753 | |
James Y Knight | d9e85a0 | 2019-02-01 20:43:34 +0000 | [diff] [blame] | 754 | auto *NewII = InvokeInst::Create(II->getFunctionType(), II->getCalledValue(), |
| 755 | II->getNormalDest(), II->getUnwindDest(), |
| 756 | Args, OpB, II->getName(), InsertPt); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 757 | NewII->setCallingConv(II->getCallingConv()); |
| 758 | NewII->SubclassOptionalData = II->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 759 | NewII->setAttributes(II->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 760 | NewII->setDebugLoc(II->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 761 | return NewII; |
| 762 | } |
| 763 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 764 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 765 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 766 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 767 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 768 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 769 | //===----------------------------------------------------------------------===// |
Craig Topper | 784929d | 2019-02-08 20:48:56 +0000 | [diff] [blame] | 770 | // CallBrInst Implementation |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
| 773 | void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough, |
| 774 | ArrayRef<BasicBlock *> IndirectDests, |
| 775 | ArrayRef<Value *> Args, |
| 776 | ArrayRef<OperandBundleDef> Bundles, |
| 777 | const Twine &NameStr) { |
| 778 | this->FTy = FTy; |
| 779 | |
| 780 | assert((int)getNumOperands() == |
| 781 | ComputeNumOperands(Args.size(), IndirectDests.size(), |
| 782 | CountBundleInputs(Bundles)) && |
| 783 | "NumOperands not set up?"); |
| 784 | NumIndirectDests = IndirectDests.size(); |
| 785 | setDefaultDest(Fallthrough); |
| 786 | for (unsigned i = 0; i != NumIndirectDests; ++i) |
| 787 | setIndirectDest(i, IndirectDests[i]); |
| 788 | setCalledOperand(Fn); |
| 789 | |
| 790 | #ifndef NDEBUG |
| 791 | assert(((Args.size() == FTy->getNumParams()) || |
| 792 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
| 793 | "Calling a function with bad signature"); |
| 794 | |
| 795 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
| 796 | assert((i >= FTy->getNumParams() || |
| 797 | FTy->getParamType(i) == Args[i]->getType()) && |
| 798 | "Calling a function with a bad signature!"); |
| 799 | #endif |
| 800 | |
| 801 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 802 | |
| 803 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 804 | (void)It; |
| 805 | assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!"); |
| 806 | |
| 807 | setName(NameStr); |
| 808 | } |
| 809 | |
| 810 | CallBrInst::CallBrInst(const CallBrInst &CBI) |
| 811 | : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, |
| 812 | OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), |
| 813 | CBI.getNumOperands()) { |
| 814 | setCallingConv(CBI.getCallingConv()); |
| 815 | std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); |
| 816 | std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), |
| 817 | bundle_op_info_begin()); |
| 818 | SubclassOptionalData = CBI.SubclassOptionalData; |
| 819 | NumIndirectDests = CBI.NumIndirectDests; |
| 820 | } |
| 821 | |
| 822 | CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, |
| 823 | Instruction *InsertPt) { |
| 824 | std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); |
| 825 | |
| 826 | auto *NewCBI = CallBrInst::Create(CBI->getFunctionType(), |
| 827 | CBI->getCalledValue(), |
| 828 | CBI->getDefaultDest(), |
| 829 | CBI->getIndirectDests(), |
| 830 | Args, OpB, CBI->getName(), InsertPt); |
| 831 | NewCBI->setCallingConv(CBI->getCallingConv()); |
| 832 | NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; |
| 833 | NewCBI->setAttributes(CBI->getAttributes()); |
| 834 | NewCBI->setDebugLoc(CBI->getDebugLoc()); |
| 835 | NewCBI->NumIndirectDests = CBI->NumIndirectDests; |
| 836 | return NewCBI; |
| 837 | } |
| 838 | |
| 839 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 840 | // ReturnInst Implementation |
| 841 | //===----------------------------------------------------------------------===// |
| 842 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 843 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 844 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
| 845 | OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), |
| 846 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 847 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 848 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 849 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 852 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 853 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 854 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 855 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 856 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 857 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 858 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 859 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 860 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 861 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 862 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 863 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 864 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 865 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 866 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 867 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 868 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 869 | : Instruction(Type::getVoidTy(Context), Instruction::Ret, |
| 870 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 871 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 872 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 873 | // ResumeInst Implementation |
| 874 | //===----------------------------------------------------------------------===// |
| 875 | |
| 876 | ResumeInst::ResumeInst(const ResumeInst &RI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 877 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 878 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 879 | Op<0>() = RI.Op<0>(); |
| 880 | } |
| 881 | |
| 882 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 883 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 884 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 885 | Op<0>() = Exn; |
| 886 | } |
| 887 | |
| 888 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 889 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 890 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 891 | Op<0>() = Exn; |
| 892 | } |
| 893 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 894 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 895 | // CleanupReturnInst Implementation |
| 896 | //===----------------------------------------------------------------------===// |
| 897 | |
| 898 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 899 | : Instruction(CRI.getType(), Instruction::CleanupRet, |
| 900 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 901 | CRI.getNumOperands(), |
| 902 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 903 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 904 | Op<0>() = CRI.Op<0>(); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 905 | if (CRI.hasUnwindDest()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 906 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 907 | } |
| 908 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 909 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 910 | if (UnwindBB) |
| 911 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 912 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 913 | Op<0>() = CleanupPad; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 914 | if (UnwindBB) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 915 | Op<1>() = UnwindBB; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 916 | } |
| 917 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 918 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 919 | unsigned Values, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 920 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 921 | Instruction::CleanupRet, |
| 922 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 923 | Values, InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 924 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 925 | } |
| 926 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 927 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 928 | unsigned Values, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 929 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 930 | Instruction::CleanupRet, |
| 931 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 932 | Values, InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 933 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 934 | } |
| 935 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 936 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 937 | // CatchReturnInst Implementation |
| 938 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 939 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 940 | Op<0>() = CatchPad; |
| 941 | Op<1>() = BB; |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 942 | } |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 943 | |
| 944 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 945 | : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
| 946 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 947 | Op<0>() = CRI.Op<0>(); |
| 948 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 949 | } |
| 950 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 951 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 952 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 953 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 954 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 955 | InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 956 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 957 | } |
| 958 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 959 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 960 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 961 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 962 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 963 | InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 964 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 965 | } |
| 966 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 967 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 968 | // CatchSwitchInst Implementation |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 969 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 970 | |
| 971 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 972 | unsigned NumReservedValues, |
| 973 | const Twine &NameStr, |
| 974 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 975 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 976 | InsertBefore) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 977 | if (UnwindDest) |
| 978 | ++NumReservedValues; |
| 979 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 980 | setName(NameStr); |
| 981 | } |
| 982 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 983 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 984 | unsigned NumReservedValues, |
| 985 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 986 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 987 | InsertAtEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 988 | if (UnwindDest) |
| 989 | ++NumReservedValues; |
| 990 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
| 991 | setName(NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 992 | } |
| 993 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 994 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 995 | : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, |
| 996 | CSI.getNumOperands()) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 997 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); |
| 998 | setNumHungOffUseOperands(ReservedSpace); |
| 999 | Use *OL = getOperandList(); |
| 1000 | const Use *InOL = CSI.getOperandList(); |
| 1001 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) |
| 1002 | OL[I] = InOL[I]; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1003 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1004 | |
| 1005 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, |
| 1006 | unsigned NumReservedValues) { |
| 1007 | assert(ParentPad && NumReservedValues); |
| 1008 | |
| 1009 | ReservedSpace = NumReservedValues; |
| 1010 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); |
| 1011 | allocHungoffUses(ReservedSpace); |
| 1012 | |
| 1013 | Op<0>() = ParentPad; |
| 1014 | if (UnwindDest) { |
| 1015 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 1016 | setUnwindDest(UnwindDest); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /// growOperands - grow operands - This grows the operand list in response to a |
| 1021 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 1022 | void CatchSwitchInst::growOperands(unsigned Size) { |
| 1023 | unsigned NumOperands = getNumOperands(); |
| 1024 | assert(NumOperands >= 1); |
| 1025 | if (ReservedSpace >= NumOperands + Size) |
| 1026 | return; |
| 1027 | ReservedSpace = (NumOperands + Size / 2) * 2; |
| 1028 | growHungoffUses(ReservedSpace); |
| 1029 | } |
| 1030 | |
| 1031 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { |
| 1032 | unsigned OpNo = getNumOperands(); |
| 1033 | growOperands(1); |
| 1034 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 1035 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 1036 | getOperandList()[OpNo] = Handler; |
| 1037 | } |
| 1038 | |
Joseph Tremoulet | 0d80888 | 2016-01-05 02:37:41 +0000 | [diff] [blame] | 1039 | void CatchSwitchInst::removeHandler(handler_iterator HI) { |
| 1040 | // Move all subsequent handlers up one. |
| 1041 | Use *EndDst = op_end() - 1; |
| 1042 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) |
| 1043 | *CurDst = *(CurDst + 1); |
| 1044 | // Null out the last handler use. |
| 1045 | *EndDst = nullptr; |
| 1046 | |
| 1047 | setNumHungOffUseOperands(getNumOperands() - 1); |
| 1048 | } |
| 1049 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1050 | //===----------------------------------------------------------------------===// |
| 1051 | // FuncletPadInst Implementation |
| 1052 | //===----------------------------------------------------------------------===// |
| 1053 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, |
| 1054 | const Twine &NameStr) { |
| 1055 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 1056 | llvm::copy(Args, op_begin()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1057 | setParentPad(ParentPad); |
| 1058 | setName(NameStr); |
| 1059 | } |
| 1060 | |
| 1061 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) |
| 1062 | : Instruction(FPI.getType(), FPI.getOpcode(), |
| 1063 | OperandTraits<FuncletPadInst>::op_end(this) - |
| 1064 | FPI.getNumOperands(), |
| 1065 | FPI.getNumOperands()) { |
| 1066 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); |
| 1067 | setParentPad(FPI.getParentPad()); |
| 1068 | } |
| 1069 | |
| 1070 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 1071 | ArrayRef<Value *> Args, unsigned Values, |
| 1072 | const Twine &NameStr, Instruction *InsertBefore) |
| 1073 | : Instruction(ParentPad->getType(), Op, |
| 1074 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 1075 | InsertBefore) { |
| 1076 | init(ParentPad, Args, NameStr); |
| 1077 | } |
| 1078 | |
| 1079 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 1080 | ArrayRef<Value *> Args, unsigned Values, |
| 1081 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1082 | : Instruction(ParentPad->getType(), Op, |
| 1083 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 1084 | InsertAtEnd) { |
| 1085 | init(ParentPad, Args, NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1089 | // UnreachableInst Implementation |
| 1090 | //===----------------------------------------------------------------------===// |
| 1091 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1092 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1093 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1094 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 1095 | 0, InsertBefore) {} |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1096 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1097 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 1098 | 0, InsertAtEnd) {} |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1100 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1101 | // BranchInst Implementation |
| 1102 | //===----------------------------------------------------------------------===// |
| 1103 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1104 | void BranchInst::AssertOK() { |
| 1105 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1106 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1107 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1110 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1111 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1112 | OperandTraits<BranchInst>::op_end(this) - 1, 1, |
| 1113 | InsertBefore) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1114 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1115 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1116 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1118 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 1119 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1120 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1121 | OperandTraits<BranchInst>::op_end(this) - 3, 3, |
| 1122 | InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1123 | Op<-1>() = IfTrue; |
| 1124 | Op<-2>() = IfFalse; |
| 1125 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1126 | #ifndef NDEBUG |
| 1127 | AssertOK(); |
| 1128 | #endif |
| 1129 | } |
| 1130 | |
| 1131 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1132 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1133 | OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1134 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1135 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1139 | BasicBlock *InsertAtEnd) |
| 1140 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1141 | OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1142 | Op<-1>() = IfTrue; |
| 1143 | Op<-2>() = IfFalse; |
| 1144 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1145 | #ifndef NDEBUG |
| 1146 | AssertOK(); |
| 1147 | #endif |
| 1148 | } |
| 1149 | |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1150 | BranchInst::BranchInst(const BranchInst &BI) |
| 1151 | : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, |
| 1152 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 1153 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1154 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1155 | if (BI.getNumOperands() != 1) { |
| 1156 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1157 | Op<-3>() = BI.Op<-3>(); |
| 1158 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1159 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1160 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1163 | void BranchInst::swapSuccessors() { |
| 1164 | assert(isConditional() && |
| 1165 | "Cannot swap successors of an unconditional branch"); |
| 1166 | Op<-1>().swap(Op<-2>()); |
| 1167 | |
| 1168 | // Update profile metadata if present and it matches our structural |
| 1169 | // expectations. |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 1170 | swapProfMetadata(); |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1173 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1174 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1175 | //===----------------------------------------------------------------------===// |
| 1176 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1177 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1178 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1179 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1180 | else { |
| 1181 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1182 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1183 | assert(Amt->getType()->isIntegerTy() && |
| 1184 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1185 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1186 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1189 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1190 | Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1191 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1192 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1193 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1194 | BasicBlock *InsertAtEnd) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1195 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1196 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1197 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1198 | const Twine &Name, Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1199 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
| 1200 | |
| 1201 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1202 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1203 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
| 1204 | |
| 1205 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1206 | unsigned Align, const Twine &Name, |
| 1207 | Instruction *InsertBefore) |
| 1208 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1209 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1210 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1211 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1212 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1213 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1216 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1217 | unsigned Align, const Twine &Name, |
| 1218 | BasicBlock *InsertAtEnd) |
| 1219 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1220 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1221 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1222 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1223 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1224 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1227 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1228 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1229 | assert(Align <= MaximumAlignment && |
| 1230 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1231 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1232 | (Log2_32(Align) + 1)); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1233 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1234 | } |
| 1235 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1236 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1237 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1238 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1239 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1242 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1243 | /// function and is a constant size. If so, the code generator will fold it |
| 1244 | /// into the prolog/epilog code, so it is basically free. |
| 1245 | bool AllocaInst::isStaticAlloca() const { |
| 1246 | // Must be constant size. |
| 1247 | if (!isa<ConstantInt>(getArraySize())) return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1249 | // Must be in the entry block. |
| 1250 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1251 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1254 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1255 | // LoadInst Implementation |
| 1256 | //===----------------------------------------------------------------------===// |
| 1257 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1258 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1259 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1260 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1261 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1262 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1265 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1266 | Instruction *InsertBef) |
| 1267 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1268 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1269 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1270 | BasicBlock *InsertAE) |
| 1271 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1272 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1273 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1274 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1275 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1276 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1277 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1278 | BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1279 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1280 | |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1281 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1282 | unsigned Align, Instruction *InsertBef) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1283 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1284 | SyncScope::System, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1285 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1286 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1287 | unsigned Align, BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1288 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1289 | SyncScope::System, InsertAE) {} |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1290 | |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1291 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1292 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1293 | SyncScope::ID SSID, Instruction *InsertBef) |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1294 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | 79009f8 | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1295 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1296 | setVolatile(isVolatile); |
| 1297 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1298 | setAtomic(Order, SSID); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1299 | AssertOK(); |
| 1300 | setName(Name); |
| 1301 | } |
| 1302 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1303 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
| 1304 | unsigned Align, AtomicOrdering Order, SyncScope::ID SSID, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1305 | BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1306 | : UnaryInstruction(Ty, Load, Ptr, InsertAE) { |
| 1307 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1308 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1309 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1310 | setAtomic(Order, SSID); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1311 | AssertOK(); |
| 1312 | setName(Name); |
| 1313 | } |
| 1314 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1315 | void LoadInst::setAlignment(unsigned Align) { |
| 1316 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1317 | assert(Align <= MaximumAlignment && |
| 1318 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1319 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1320 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1321 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1322 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1323 | |
| 1324 | //===----------------------------------------------------------------------===// |
| 1325 | // StoreInst Implementation |
| 1326 | //===----------------------------------------------------------------------===// |
| 1327 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1328 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1329 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1330 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1331 | "Ptr must have pointer type!"); |
| 1332 | assert(getOperand(0)->getType() == |
| 1333 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1334 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1335 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | 1d7c97e | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1336 | "Alignment required for atomic store"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1337 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1339 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1340 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1341 | |
| 1342 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1343 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1344 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1345 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1346 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1347 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1348 | |
| 1349 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1350 | BasicBlock *InsertAtEnd) |
| 1351 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1352 | |
| 1353 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1354 | Instruction *InsertBefore) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1355 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1356 | SyncScope::System, InsertBefore) {} |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1357 | |
| 1358 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1359 | BasicBlock *InsertAtEnd) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1360 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1361 | SyncScope::System, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1362 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1363 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1364 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1365 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1366 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1367 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1368 | OperandTraits<StoreInst>::op_begin(this), |
| 1369 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1370 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1371 | Op<0>() = val; |
| 1372 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1373 | setVolatile(isVolatile); |
| 1374 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1375 | setAtomic(Order, SSID); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1376 | AssertOK(); |
| 1377 | } |
| 1378 | |
| 1379 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1380 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1381 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1382 | BasicBlock *InsertAtEnd) |
| 1383 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1384 | OperandTraits<StoreInst>::op_begin(this), |
| 1385 | OperandTraits<StoreInst>::operands(this), |
| 1386 | InsertAtEnd) { |
| 1387 | Op<0>() = val; |
| 1388 | Op<1>() = addr; |
| 1389 | setVolatile(isVolatile); |
| 1390 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1391 | setAtomic(Order, SSID); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1392 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1395 | void StoreInst::setAlignment(unsigned Align) { |
| 1396 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1397 | assert(Align <= MaximumAlignment && |
| 1398 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1399 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1400 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1401 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1404 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1405 | // AtomicCmpXchgInst Implementation |
| 1406 | //===----------------------------------------------------------------------===// |
| 1407 | |
| 1408 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1409 | AtomicOrdering SuccessOrdering, |
| 1410 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1411 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1412 | Op<0>() = Ptr; |
| 1413 | Op<1>() = Cmp; |
| 1414 | Op<2>() = NewVal; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1415 | setSuccessOrdering(SuccessOrdering); |
| 1416 | setFailureOrdering(FailureOrdering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1417 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1418 | |
| 1419 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1420 | "All operands must be non-null!"); |
| 1421 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1422 | "Ptr must have pointer type!"); |
| 1423 | assert(getOperand(1)->getType() == |
| 1424 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1425 | && "Ptr must be a pointer to Cmp type!"); |
| 1426 | assert(getOperand(2)->getType() == |
| 1427 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1428 | && "Ptr must be a pointer to NewVal type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1429 | assert(SuccessOrdering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1430 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1431 | assert(FailureOrdering != AtomicOrdering::NotAtomic && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1432 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1433 | assert(!isStrongerThan(FailureOrdering, SuccessOrdering) && |
| 1434 | "AtomicCmpXchg failure argument shall be no stronger than the success " |
| 1435 | "argument"); |
| 1436 | assert(FailureOrdering != AtomicOrdering::Release && |
| 1437 | FailureOrdering != AtomicOrdering::AcquireRelease && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1438 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1442 | AtomicOrdering SuccessOrdering, |
| 1443 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1444 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1445 | Instruction *InsertBefore) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1446 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1447 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1448 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1449 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1450 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1454 | AtomicOrdering SuccessOrdering, |
| 1455 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1456 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1457 | BasicBlock *InsertAtEnd) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1458 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1459 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1460 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1461 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1462 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1463 | } |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1464 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1465 | //===----------------------------------------------------------------------===// |
| 1466 | // AtomicRMWInst Implementation |
| 1467 | //===----------------------------------------------------------------------===// |
| 1468 | |
| 1469 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1470 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1471 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1472 | Op<0>() = Ptr; |
| 1473 | Op<1>() = Val; |
| 1474 | setOperation(Operation); |
| 1475 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1476 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1477 | |
| 1478 | assert(getOperand(0) && getOperand(1) && |
| 1479 | "All operands must be non-null!"); |
| 1480 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1481 | "Ptr must have pointer type!"); |
| 1482 | assert(getOperand(1)->getType() == |
| 1483 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1484 | && "Ptr must be a pointer to Val type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1485 | assert(Ordering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1486 | "AtomicRMW instructions must be atomic!"); |
| 1487 | } |
| 1488 | |
| 1489 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1490 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1491 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1492 | Instruction *InsertBefore) |
| 1493 | : Instruction(Val->getType(), AtomicRMW, |
| 1494 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1495 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1496 | InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1497 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1501 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1502 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1503 | BasicBlock *InsertAtEnd) |
| 1504 | : Instruction(Val->getType(), AtomicRMW, |
| 1505 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1506 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1507 | InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1508 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Matt Arsenault | b02ba99 | 2018-10-02 23:44:11 +0000 | [diff] [blame] | 1511 | StringRef AtomicRMWInst::getOperationName(BinOp Op) { |
| 1512 | switch (Op) { |
| 1513 | case AtomicRMWInst::Xchg: |
| 1514 | return "xchg"; |
| 1515 | case AtomicRMWInst::Add: |
| 1516 | return "add"; |
| 1517 | case AtomicRMWInst::Sub: |
| 1518 | return "sub"; |
| 1519 | case AtomicRMWInst::And: |
| 1520 | return "and"; |
| 1521 | case AtomicRMWInst::Nand: |
| 1522 | return "nand"; |
| 1523 | case AtomicRMWInst::Or: |
| 1524 | return "or"; |
| 1525 | case AtomicRMWInst::Xor: |
| 1526 | return "xor"; |
| 1527 | case AtomicRMWInst::Max: |
| 1528 | return "max"; |
| 1529 | case AtomicRMWInst::Min: |
| 1530 | return "min"; |
| 1531 | case AtomicRMWInst::UMax: |
| 1532 | return "umax"; |
| 1533 | case AtomicRMWInst::UMin: |
| 1534 | return "umin"; |
Matt Arsenault | 3950833 | 2019-01-22 18:18:02 +0000 | [diff] [blame] | 1535 | case AtomicRMWInst::FAdd: |
| 1536 | return "fadd"; |
| 1537 | case AtomicRMWInst::FSub: |
| 1538 | return "fsub"; |
Matt Arsenault | b02ba99 | 2018-10-02 23:44:11 +0000 | [diff] [blame] | 1539 | case AtomicRMWInst::BAD_BINOP: |
| 1540 | return "<invalid operation>"; |
| 1541 | } |
| 1542 | |
| 1543 | llvm_unreachable("invalid atomicrmw operation"); |
| 1544 | } |
| 1545 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1546 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1547 | // FenceInst Implementation |
| 1548 | //===----------------------------------------------------------------------===// |
| 1549 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1550 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1551 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1552 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1553 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1554 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1555 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1558 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1559 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1560 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1561 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1562 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1563 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1567 | // GetElementPtrInst Implementation |
| 1568 | //===----------------------------------------------------------------------===// |
| 1569 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1570 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1571 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1572 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1573 | "NumOperands not initialized?"); |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1574 | Op<0>() = Ptr; |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 1575 | llvm::copy(IdxList, op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1576 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1579 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1580 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1581 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1582 | GEPI.getNumOperands(), |
| 1583 | GEPI.getNumOperands()), |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1584 | SourceElementType(GEPI.SourceElementType), |
| 1585 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1586 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1587 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1590 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1591 | /// a gep instruction with the specified parameters. |
| 1592 | /// |
| 1593 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1594 | /// indices, either as Value* or uint64_t. |
| 1595 | /// |
| 1596 | /// A null type is returned if the indices are invalid for the specified |
| 1597 | /// pointer type. |
| 1598 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1599 | template <typename IndexTy> |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1600 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1601 | // 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] | 1602 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1603 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1604 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1605 | // 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] | 1606 | // it cannot be 'stepped over'. |
| 1607 | if (!Agg->isSized()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1608 | return nullptr; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1609 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1610 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1611 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1612 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1613 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1614 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1615 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1616 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1617 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1618 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1621 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1622 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1625 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1626 | ArrayRef<Constant *> IdxList) { |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1627 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1630 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1631 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1634 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1635 | /// zeros. If so, the result pointer and the first operand have the same |
| 1636 | /// value, just potentially different types. |
| 1637 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1638 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1639 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1640 | if (!CI->isZero()) return false; |
| 1641 | } else { |
| 1642 | return false; |
| 1643 | } |
| 1644 | } |
| 1645 | return true; |
| 1646 | } |
| 1647 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1648 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1649 | /// constant integers. If so, the result pointer and the first operand have |
| 1650 | /// a constant offset between them. |
| 1651 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1652 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1653 | if (!isa<ConstantInt>(getOperand(i))) |
| 1654 | return false; |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1659 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1660 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1661 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1662 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1663 | bool GetElementPtrInst::isInBounds() const { |
| 1664 | return cast<GEPOperator>(this)->isInBounds(); |
| 1665 | } |
| 1666 | |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1667 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1668 | APInt &Offset) const { |
Chandler Carruth | 7ec41c7 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1669 | // Delegate to the generic GEPOperator implementation. |
| 1670 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1673 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1674 | // ExtractElementInst Implementation |
| 1675 | //===----------------------------------------------------------------------===// |
| 1676 | |
| 1677 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1678 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1679 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1680 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1681 | ExtractElement, |
| 1682 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1683 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1684 | assert(isValidOperands(Val, Index) && |
| 1685 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1686 | Op<0>() = Val; |
| 1687 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1688 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1692 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1693 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1694 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1695 | ExtractElement, |
| 1696 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1697 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1698 | assert(isValidOperands(Val, Index) && |
| 1699 | "Invalid extractelement instruction operands!"); |
| 1700 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1701 | Op<0>() = Val; |
| 1702 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1703 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1706 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1707 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | return true; |
| 1710 | } |
| 1711 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1712 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1713 | // InsertElementInst Implementation |
| 1714 | //===----------------------------------------------------------------------===// |
| 1715 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1716 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1717 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1718 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1719 | : Instruction(Vec->getType(), InsertElement, |
| 1720 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1721 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1722 | assert(isValidOperands(Vec, Elt, Index) && |
| 1723 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1724 | Op<0>() = Vec; |
| 1725 | Op<1>() = Elt; |
| 1726 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1727 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1730 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1731 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1732 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1733 | : Instruction(Vec->getType(), InsertElement, |
| 1734 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1735 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1736 | assert(isValidOperands(Vec, Elt, Index) && |
| 1737 | "Invalid insertelement instruction operands!"); |
| 1738 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1739 | Op<0>() = Vec; |
| 1740 | Op<1>() = Elt; |
| 1741 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1742 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1745 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1746 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1747 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1748 | return false; // First operand of insertelement must be vector type. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1749 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1750 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1751 | return false;// Second operand of insertelement must be vector element type. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1752 | |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1753 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1754 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1755 | return true; |
| 1756 | } |
| 1757 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1758 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1759 | // ShuffleVectorInst Implementation |
| 1760 | //===----------------------------------------------------------------------===// |
| 1761 | |
| 1762 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1763 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1764 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1765 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1766 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1767 | ShuffleVector, |
| 1768 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1769 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1770 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1771 | assert(isValidOperands(V1, V2, Mask) && |
| 1772 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1773 | Op<0>() = V1; |
| 1774 | Op<1>() = V2; |
| 1775 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1776 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1780 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1781 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1782 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1783 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1784 | ShuffleVector, |
| 1785 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1786 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1787 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1788 | assert(isValidOperands(V1, V2, Mask) && |
| 1789 | "Invalid shuffle vector instruction operands!"); |
| 1790 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1791 | Op<0>() = V1; |
| 1792 | Op<1>() = V2; |
| 1793 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1794 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Sanjay Patel | b276dd1 | 2019-03-31 15:01:30 +0000 | [diff] [blame] | 1797 | void ShuffleVectorInst::commute() { |
| 1798 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1799 | int NumMaskElts = getMask()->getType()->getVectorNumElements(); |
| 1800 | SmallVector<Constant*, 16> NewMask(NumMaskElts); |
| 1801 | Type *Int32Ty = Type::getInt32Ty(getContext()); |
| 1802 | for (int i = 0; i != NumMaskElts; ++i) { |
| 1803 | int MaskElt = getMaskValue(i); |
| 1804 | if (MaskElt == -1) { |
| 1805 | NewMask[i] = UndefValue::get(Int32Ty); |
| 1806 | continue; |
| 1807 | } |
| 1808 | assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask"); |
| 1809 | MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; |
| 1810 | NewMask[i] = ConstantInt::get(Int32Ty, MaskElt); |
| 1811 | } |
| 1812 | Op<2>() = ConstantVector::get(NewMask); |
| 1813 | Op<0>().swap(Op<1>()); |
| 1814 | } |
| 1815 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1816 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1817 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1818 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1819 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1820 | return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1822 | // Mask must be vector of i32. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1823 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1824 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1825 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1826 | |
| 1827 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1828 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1829 | return true; |
| 1830 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1831 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1832 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1833 | for (Value *Op : MV->operands()) { |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1834 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1835 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1836 | return false; |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1837 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1838 | return false; |
| 1839 | } |
| 1840 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1841 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1842 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1843 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1844 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1845 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1846 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1847 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1848 | return false; |
| 1849 | return true; |
| 1850 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1852 | // The bitcode reader can create a place holder for a forward reference |
| 1853 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1854 | // fall into this case and fail. To avoid this error, do this bit of |
| 1855 | // ugliness to allow such a mask pass. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1856 | if (const auto *CE = dyn_cast<ConstantExpr>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1857 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1858 | return true; |
| 1859 | |
| 1860 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1863 | int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) { |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1864 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1865 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1866 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1867 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1868 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1869 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1870 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1873 | void ShuffleVectorInst::getShuffleMask(const Constant *Mask, |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1874 | SmallVectorImpl<int> &Result) { |
| 1875 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1876 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1877 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1878 | for (unsigned i = 0; i != NumElts; ++i) |
| 1879 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1880 | return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1881 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1882 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1883 | Constant *C = Mask->getAggregateElement(i); |
| 1884 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1885 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1889 | static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1890 | assert(!Mask.empty() && "Shuffle mask must contain elements"); |
| 1891 | bool UsesLHS = false; |
| 1892 | bool UsesRHS = false; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1893 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1894 | if (Mask[i] == -1) |
| 1895 | continue; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1896 | assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) && |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1897 | "Out-of-bounds shuffle mask element"); |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1898 | UsesLHS |= (Mask[i] < NumOpElts); |
| 1899 | UsesRHS |= (Mask[i] >= NumOpElts); |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1900 | if (UsesLHS && UsesRHS) |
| 1901 | return false; |
| 1902 | } |
| 1903 | assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source"); |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1907 | bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { |
| 1908 | // We don't have vector operand size information, so assume operands are the |
| 1909 | // same size as the mask. |
| 1910 | return isSingleSourceMaskImpl(Mask, Mask.size()); |
| 1911 | } |
| 1912 | |
| 1913 | static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
| 1914 | if (!isSingleSourceMaskImpl(Mask, NumOpElts)) |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1915 | return false; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1916 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1917 | if (Mask[i] == -1) |
| 1918 | continue; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1919 | if (Mask[i] != i && Mask[i] != (NumOpElts + i)) |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1925 | bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { |
| 1926 | // We don't have vector operand size information, so assume operands are the |
| 1927 | // same size as the mask. |
| 1928 | return isIdentityMaskImpl(Mask, Mask.size()); |
| 1929 | } |
| 1930 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1931 | bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { |
| 1932 | if (!isSingleSourceMask(Mask)) |
| 1933 | return false; |
| 1934 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1935 | if (Mask[i] == -1) |
| 1936 | continue; |
| 1937 | if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) |
| 1938 | return false; |
| 1939 | } |
| 1940 | return true; |
| 1941 | } |
| 1942 | |
| 1943 | bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { |
| 1944 | if (!isSingleSourceMask(Mask)) |
| 1945 | return false; |
| 1946 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1947 | if (Mask[i] == -1) |
| 1948 | continue; |
| 1949 | if (Mask[i] != 0 && Mask[i] != NumElts) |
| 1950 | return false; |
| 1951 | } |
| 1952 | return true; |
| 1953 | } |
| 1954 | |
| 1955 | bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { |
| 1956 | // Select is differentiated from identity. It requires using both sources. |
| 1957 | if (isSingleSourceMask(Mask)) |
| 1958 | return false; |
| 1959 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1960 | if (Mask[i] == -1) |
| 1961 | continue; |
| 1962 | if (Mask[i] != i && Mask[i] != (NumElts + i)) |
| 1963 | return false; |
| 1964 | } |
| 1965 | return true; |
| 1966 | } |
| 1967 | |
| 1968 | bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { |
| 1969 | // Example masks that will return true: |
| 1970 | // v1 = <a, b, c, d> |
| 1971 | // v2 = <e, f, g, h> |
| 1972 | // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> |
| 1973 | // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> |
| 1974 | |
| 1975 | // 1. The number of elements in the mask must be a power-of-2 and at least 2. |
| 1976 | int NumElts = Mask.size(); |
| 1977 | if (NumElts < 2 || !isPowerOf2_32(NumElts)) |
| 1978 | return false; |
| 1979 | |
| 1980 | // 2. The first element of the mask must be either a 0 or a 1. |
| 1981 | if (Mask[0] != 0 && Mask[0] != 1) |
| 1982 | return false; |
| 1983 | |
| 1984 | // 3. The difference between the first 2 elements must be equal to the |
| 1985 | // number of elements in the mask. |
| 1986 | if ((Mask[1] - Mask[0]) != NumElts) |
| 1987 | return false; |
| 1988 | |
| 1989 | // 4. The difference between consecutive even-numbered and odd-numbered |
| 1990 | // elements must be equal to 2. |
| 1991 | for (int i = 2; i < NumElts; ++i) { |
| 1992 | int MaskEltVal = Mask[i]; |
| 1993 | if (MaskEltVal == -1) |
| 1994 | return false; |
| 1995 | int MaskEltPrevVal = Mask[i - 2]; |
| 1996 | if (MaskEltVal - MaskEltPrevVal != 2) |
| 1997 | return false; |
| 1998 | } |
| 1999 | return true; |
| 2000 | } |
| 2001 | |
Simon Pilgrim | d0c7160 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 2002 | bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, |
| 2003 | int NumSrcElts, int &Index) { |
| 2004 | // Must extract from a single source. |
| 2005 | if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) |
| 2006 | return false; |
| 2007 | |
| 2008 | // Must be smaller (else this is an Identity shuffle). |
Fangrui Song | 4f2e66c | 2018-11-09 16:45:37 +0000 | [diff] [blame] | 2009 | if (NumSrcElts <= (int)Mask.size()) |
Simon Pilgrim | d0c7160 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 2010 | return false; |
| 2011 | |
| 2012 | // Find start of extraction, accounting that we may start with an UNDEF. |
| 2013 | int SubIndex = -1; |
| 2014 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 2015 | int M = Mask[i]; |
| 2016 | if (M < 0) |
| 2017 | continue; |
| 2018 | int Offset = (M % NumSrcElts) - i; |
| 2019 | if (0 <= SubIndex && SubIndex != Offset) |
| 2020 | return false; |
| 2021 | SubIndex = Offset; |
| 2022 | } |
| 2023 | |
| 2024 | if (0 <= SubIndex) { |
| 2025 | Index = SubIndex; |
| 2026 | return true; |
| 2027 | } |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 2031 | bool ShuffleVectorInst::isIdentityWithPadding() const { |
| 2032 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 2033 | int NumMaskElts = getType()->getVectorNumElements(); |
| 2034 | if (NumMaskElts <= NumOpElts) |
| 2035 | return false; |
| 2036 | |
| 2037 | // The first part of the mask must choose elements from exactly 1 source op. |
Sanjay Patel | 8d39ed8 | 2018-08-30 16:44:07 +0000 | [diff] [blame] | 2038 | SmallVector<int, 16> Mask = getShuffleMask(); |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 2039 | if (!isIdentityMaskImpl(Mask, NumOpElts)) |
| 2040 | return false; |
| 2041 | |
| 2042 | // All extending must be with undef elements. |
| 2043 | for (int i = NumOpElts; i < NumMaskElts; ++i) |
| 2044 | if (Mask[i] != -1) |
| 2045 | return false; |
| 2046 | |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | bool ShuffleVectorInst::isIdentityWithExtract() const { |
| 2051 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 2052 | int NumMaskElts = getType()->getVectorNumElements(); |
| 2053 | if (NumMaskElts >= NumOpElts) |
| 2054 | return false; |
| 2055 | |
| 2056 | return isIdentityMaskImpl(getShuffleMask(), NumOpElts); |
| 2057 | } |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 2058 | |
Sanjay Patel | fd4976b | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 2059 | bool ShuffleVectorInst::isConcat() const { |
| 2060 | // Vector concatenation is differentiated from identity with padding. |
| 2061 | if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>())) |
| 2062 | return false; |
| 2063 | |
| 2064 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 2065 | int NumMaskElts = getType()->getVectorNumElements(); |
| 2066 | if (NumMaskElts != NumOpElts * 2) |
| 2067 | return false; |
| 2068 | |
| 2069 | // Use the mask length rather than the operands' vector lengths here. We |
| 2070 | // already know that the shuffle returns a vector twice as long as the inputs, |
| 2071 | // and neither of the inputs are undef vectors. If the mask picks consecutive |
| 2072 | // elements from both inputs, then this is a concatenation of the inputs. |
| 2073 | return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); |
| 2074 | } |
| 2075 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2076 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2077 | // InsertValueInst Class |
| 2078 | //===----------------------------------------------------------------------===// |
| 2079 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2080 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2081 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 2082 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2083 | |
| 2084 | // There's no fundamental reason why we require at least one index |
| 2085 | // (other than weirdness with &*IdxBegin being invalid; see |
| 2086 | // getelementptr's init routine for example). But there's no |
| 2087 | // present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2088 | assert(!Idxs.empty() && "InsertValueInst must have at least one index"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2089 | |
| 2090 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2091 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2092 | Op<0>() = Agg; |
| 2093 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2094 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2095 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 2096 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 2100 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2101 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 2102 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 2103 | Op<0>() = IVI.getOperand(0); |
| 2104 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2105 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2109 | // ExtractValueInst Class |
| 2110 | //===----------------------------------------------------------------------===// |
| 2111 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2112 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 2113 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2114 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2115 | // There's no fundamental reason why we require at least one index. |
| 2116 | // But there's no present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2117 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2118 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2119 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 2120 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 2124 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2125 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2126 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2129 | // getIndexedType - Returns the type of the element that would be extracted |
| 2130 | // with an extractvalue instruction with the specified parameters. |
| 2131 | // |
| 2132 | // A null type is returned if the indices are invalid for the specified |
| 2133 | // pointer type. |
| 2134 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2135 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2136 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 2137 | for (unsigned Index : Idxs) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2138 | // We can't use CompositeType::indexValid(Index) here. |
| 2139 | // indexValid() always returns true for arrays because getelementptr allows |
| 2140 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 2141 | // insertvalue we need to check array indexing manually. |
| 2142 | // Since the only other types we can index into are struct types it's just |
| 2143 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2144 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2145 | if (Index >= AT->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2146 | return nullptr; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2147 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2148 | if (Index >= ST->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2149 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2150 | } else { |
| 2151 | // Not a valid type to index into. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2152 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2156 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2157 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2158 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2159 | |
| 2160 | //===----------------------------------------------------------------------===// |
Cameron McInally | cbde0d9 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 2161 | // UnaryOperator Class |
| 2162 | //===----------------------------------------------------------------------===// |
| 2163 | |
| 2164 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2165 | Type *Ty, const Twine &Name, |
| 2166 | Instruction *InsertBefore) |
| 2167 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
| 2168 | Op<0>() = S; |
| 2169 | setName(Name); |
| 2170 | AssertOK(); |
| 2171 | } |
| 2172 | |
| 2173 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2174 | Type *Ty, const Twine &Name, |
| 2175 | BasicBlock *InsertAtEnd) |
| 2176 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
| 2177 | Op<0>() = S; |
| 2178 | setName(Name); |
| 2179 | AssertOK(); |
| 2180 | } |
| 2181 | |
| 2182 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2183 | const Twine &Name, |
| 2184 | Instruction *InsertBefore) { |
| 2185 | return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); |
| 2186 | } |
| 2187 | |
| 2188 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2189 | const Twine &Name, |
| 2190 | BasicBlock *InsertAtEnd) { |
| 2191 | UnaryOperator *Res = Create(Op, S, Name); |
| 2192 | InsertAtEnd->getInstList().push_back(Res); |
| 2193 | return Res; |
| 2194 | } |
| 2195 | |
| 2196 | void UnaryOperator::AssertOK() { |
| 2197 | Value *LHS = getOperand(0); |
| 2198 | (void)LHS; // Silence warnings. |
| 2199 | #ifndef NDEBUG |
| 2200 | switch (getOpcode()) { |
| 2201 | case FNeg: |
| 2202 | assert(getType() == LHS->getType() && |
| 2203 | "Unary operation should return same type as operand!"); |
| 2204 | assert(getType()->isFPOrFPVectorTy() && |
| 2205 | "Tried to create a floating-point operation on a " |
| 2206 | "non-floating-point type!"); |
| 2207 | break; |
| 2208 | default: llvm_unreachable("Invalid opcode provided"); |
| 2209 | } |
| 2210 | #endif |
| 2211 | } |
| 2212 | |
| 2213 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2214 | // BinaryOperator Class |
| 2215 | //===----------------------------------------------------------------------===// |
| 2216 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2217 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2218 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2219 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2220 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2221 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2222 | OperandTraits<BinaryOperator>::operands(this), |
| 2223 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2224 | Op<0>() = S1; |
| 2225 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2226 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2227 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2230 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2231 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2232 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2233 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2234 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2235 | OperandTraits<BinaryOperator>::operands(this), |
| 2236 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2237 | Op<0>() = S1; |
| 2238 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2239 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2240 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2243 | void BinaryOperator::AssertOK() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2244 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 2245 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2246 | assert(LHS->getType() == RHS->getType() && |
| 2247 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2248 | #ifndef NDEBUG |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2249 | switch (getOpcode()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2250 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2251 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2252 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2253 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2254 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2255 | "Tried to create an integer operation on a non-integer type!"); |
| 2256 | break; |
| 2257 | case FAdd: case FSub: |
| 2258 | case FMul: |
| 2259 | assert(getType() == LHS->getType() && |
| 2260 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2261 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2262 | "Tried to create a floating-point operation on a " |
| 2263 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2264 | break; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2265 | case UDiv: |
| 2266 | case SDiv: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2267 | assert(getType() == LHS->getType() && |
| 2268 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2269 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2270 | "Incorrect operand type (not integer) for S/UDIV"); |
| 2271 | break; |
| 2272 | case FDiv: |
| 2273 | assert(getType() == LHS->getType() && |
| 2274 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2275 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2276 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2277 | break; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2278 | case URem: |
| 2279 | case SRem: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2280 | assert(getType() == LHS->getType() && |
| 2281 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2282 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2283 | "Incorrect operand type (not integer) for S/UREM"); |
| 2284 | break; |
| 2285 | case FRem: |
| 2286 | assert(getType() == LHS->getType() && |
| 2287 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2288 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2289 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2290 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2291 | case Shl: |
| 2292 | case LShr: |
| 2293 | case AShr: |
| 2294 | assert(getType() == LHS->getType() && |
| 2295 | "Shift operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2296 | assert(getType()->isIntOrIntVectorTy() && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2297 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2298 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2299 | case And: case Or: |
| 2300 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2301 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2302 | "Logical operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2303 | assert(getType()->isIntOrIntVectorTy() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2304 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2305 | break; |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2306 | default: llvm_unreachable("Invalid opcode provided"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2307 | } |
| 2308 | #endif |
| 2309 | } |
| 2310 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2311 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2312 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2313 | Instruction *InsertBefore) { |
| 2314 | assert(S1->getType() == S2->getType() && |
| 2315 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2316 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2319 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2320 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2321 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2322 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2323 | InsertAtEnd->getInstList().push_back(Res); |
| 2324 | return Res; |
| 2325 | } |
| 2326 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2327 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2328 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2329 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2330 | return new BinaryOperator(Instruction::Sub, |
| 2331 | zero, Op, |
| 2332 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2335 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2336 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2337 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2338 | return new BinaryOperator(Instruction::Sub, |
| 2339 | zero, Op, |
| 2340 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2343 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2344 | Instruction *InsertBefore) { |
| 2345 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2346 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2347 | } |
| 2348 | |
| 2349 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2350 | BasicBlock *InsertAtEnd) { |
| 2351 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2352 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2353 | } |
| 2354 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2355 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2356 | Instruction *InsertBefore) { |
| 2357 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2358 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2359 | } |
| 2360 | |
| 2361 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2362 | BasicBlock *InsertAtEnd) { |
| 2363 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2364 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2365 | } |
| 2366 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2367 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2368 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2369 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2370 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2371 | Op->getType(), Name, InsertBefore); |
| 2372 | } |
| 2373 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2374 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2375 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2376 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2377 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2378 | Op->getType(), Name, InsertAtEnd); |
| 2379 | } |
| 2380 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2381 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2382 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2383 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2384 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2385 | Op->getType(), Name, InsertBefore); |
| 2386 | } |
| 2387 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2388 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2389 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2390 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2391 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2392 | Op->getType(), Name, InsertAtEnd); |
| 2393 | } |
| 2394 | |
Sanjay Patel | 4ce99d4 | 2016-11-16 18:09:44 +0000 | [diff] [blame] | 2395 | // Exchange the two operands to this instruction. This instruction is safe to |
| 2396 | // use on any binary instruction and does not modify the semantics of the |
| 2397 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode |
| 2398 | // is changed. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2399 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2400 | if (!isCommutative()) |
| 2401 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2402 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2403 | return false; |
| 2404 | } |
| 2405 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2406 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2407 | // FPMathOperator Class |
| 2408 | //===----------------------------------------------------------------------===// |
| 2409 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2410 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2411 | const MDNode *MD = |
| 2412 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2413 | if (!MD) |
| 2414 | return 0.0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2415 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2416 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2419 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2420 | // CastInst Class |
| 2421 | //===----------------------------------------------------------------------===// |
| 2422 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2423 | // Just determine if this cast only deals with integral->integral conversion. |
| 2424 | bool CastInst::isIntegerCast() const { |
| 2425 | switch (getOpcode()) { |
| 2426 | default: return false; |
| 2427 | case Instruction::ZExt: |
| 2428 | case Instruction::SExt: |
| 2429 | case Instruction::Trunc: |
| 2430 | return true; |
| 2431 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2432 | return getOperand(0)->getType()->isIntegerTy() && |
| 2433 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2434 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2437 | bool CastInst::isLosslessCast() const { |
| 2438 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2439 | if (getOpcode() != Instruction::BitCast) |
| 2440 | return false; |
| 2441 | |
| 2442 | // Identity cast is always lossless |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2443 | Type *SrcTy = getOperand(0)->getType(); |
| 2444 | Type *DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2445 | if (SrcTy == DstTy) |
| 2446 | return true; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2447 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2448 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2449 | if (SrcTy->isPointerTy()) |
| 2450 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2451 | return false; // Other types have no identity values |
| 2452 | } |
| 2453 | |
| 2454 | /// This function determines if the CastInst does not require any bits to be |
| 2455 | /// changed in order to effect the cast. Essentially, it identifies cases where |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2456 | /// 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] | 2457 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2458 | /// # bitcast i32* %x to i8* |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2459 | /// # bitcast <2 x i32> %x to <4 x i16> |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2460 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2461 | /// Determine if the described cast is a no-op. |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2462 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2463 | Type *SrcTy, |
| 2464 | Type *DestTy, |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2465 | const DataLayout &DL) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2466 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2467 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2468 | case Instruction::Trunc: |
| 2469 | case Instruction::ZExt: |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2470 | case Instruction::SExt: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2471 | case Instruction::FPTrunc: |
| 2472 | case Instruction::FPExt: |
| 2473 | case Instruction::UIToFP: |
| 2474 | case Instruction::SIToFP: |
| 2475 | case Instruction::FPToUI: |
| 2476 | case Instruction::FPToSI: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2477 | case Instruction::AddrSpaceCast: |
| 2478 | // TODO: Target informations may give a more accurate answer here. |
| 2479 | return false; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2480 | case Instruction::BitCast: |
| 2481 | return true; // BitCast never modifies bits. |
| 2482 | case Instruction::PtrToInt: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2483 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2484 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2485 | case Instruction::IntToPtr: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2486 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2487 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2488 | } |
| 2489 | } |
| 2490 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2491 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Mikael Holmen | 6efe507 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 2492 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | /// This function determines if a pair of casts can be eliminated and what |
| 2496 | /// 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] | 2497 | /// instructions like this: |
| 2498 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2499 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2500 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2501 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2502 | /// If no such cast is permitted, the function returns 0. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2503 | unsigned CastInst::isEliminableCastPair( |
| 2504 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2505 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2506 | Type *DstIntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2507 | // Define the 144 possibilities for these two cast instructions. The values |
| 2508 | // 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] | 2509 | // case in the switch below. The rows correspond to firstOp, the columns |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2510 | // correspond to secondOp. In looking at the table below, keep in mind |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2511 | // the following cast properties: |
| 2512 | // |
| 2513 | // Size Compare Source Destination |
| 2514 | // Operator Src ? Size Type Sign Type Sign |
| 2515 | // -------- ------------ ------------------- --------------------- |
| 2516 | // TRUNC > Integer Any Integral Any |
| 2517 | // ZEXT < Integral Unsigned Integer Any |
| 2518 | // SEXT < Integral Signed Integer Any |
| 2519 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2520 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2521 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2522 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2523 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2524 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2525 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2526 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2527 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2528 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2529 | // |
| 2530 | // 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] | 2531 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2532 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2533 | // 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] | 2534 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2535 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2536 | // same reason. |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2537 | const unsigned numCastOps = |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2538 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2539 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2540 | // T F F U S F F P I B A -+ |
| 2541 | // R Z S P P I I T P 2 N T S | |
| 2542 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2543 | // N X X U S F F N X N 2 V V | |
| 2544 | // C T T I I P P C T T P T T -+ |
| 2545 | { 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] | 2546 | { 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] | 2547 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2548 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2549 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2550 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2551 | { 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] | 2552 | { 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] | 2553 | { 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] | 2554 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2555 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2556 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2557 | { 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] | 2558 | }; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2559 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2560 | // TODO: This logic could be encoded into the table above and handled in the |
| 2561 | // switch below. |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2562 | // 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] | 2563 | // merging. However, any pair of bitcasts are allowed. |
| 2564 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); |
| 2565 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); |
| 2566 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2567 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2568 | // Check if any of the casts convert scalars <-> vectors. |
| 2569 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2570 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2571 | if (!AreBothBitcasts) |
| 2572 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2573 | |
| 2574 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2575 | [secondOp-Instruction::CastOpsBegin]; |
| 2576 | switch (ElimCase) { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2577 | case 0: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2578 | // Categorically disallowed. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2579 | return 0; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2580 | case 1: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2581 | // Allowed, use first cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2582 | return firstOp; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2583 | case 2: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2584 | // Allowed, use second cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2585 | return secondOp; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2586 | case 3: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2587 | // 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] | 2588 | // is integer and we are not converting between a vector and a |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2589 | // non-vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2590 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2591 | return firstOp; |
| 2592 | return 0; |
| 2593 | case 4: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2594 | // 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] | 2595 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2596 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2597 | return firstOp; |
| 2598 | return 0; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2599 | case 5: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2600 | // 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] | 2601 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2602 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2603 | return secondOp; |
| 2604 | return 0; |
| 2605 | case 6: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2606 | // 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] | 2607 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2608 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2609 | return secondOp; |
| 2610 | return 0; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2611 | case 7: { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2612 | // Cannot simplify if address spaces are different! |
| 2613 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2614 | return 0; |
| 2615 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2616 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2617 | // We can still fold this without knowing the actual sizes as long we |
| 2618 | // know that the intermediate pointer is the largest possible |
| 2619 | // pointer size. |
| 2620 | // FIXME: Is this always true? |
| 2621 | if (MidSize == 64) |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2622 | return Instruction::BitCast; |
| 2623 | |
| 2624 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2625 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2626 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2627 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2628 | if (MidSize >= PtrSize) |
| 2629 | return Instruction::BitCast; |
| 2630 | return 0; |
| 2631 | } |
| 2632 | case 8: { |
| 2633 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2634 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2635 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2636 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2637 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2638 | if (SrcSize == DstSize) |
| 2639 | return Instruction::BitCast; |
| 2640 | else if (SrcSize < DstSize) |
| 2641 | return firstOp; |
| 2642 | return secondOp; |
| 2643 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2644 | case 9: |
| 2645 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2646 | return Instruction::ZExt; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2647 | case 11: { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2648 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2649 | if (!MidIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2650 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2651 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2652 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2653 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2654 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2655 | return Instruction::BitCast; |
| 2656 | return 0; |
| 2657 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2658 | case 12: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2659 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2660 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2661 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2662 | return Instruction::AddrSpaceCast; |
| 2663 | return Instruction::BitCast; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2664 | case 13: |
| 2665 | // FIXME: this state can be merged with (1), but the following assert |
| 2666 | // is useful to check the correcteness of the sequence due to semantic |
| 2667 | // change of bitcast. |
| 2668 | assert( |
| 2669 | SrcTy->isPtrOrPtrVectorTy() && |
| 2670 | MidTy->isPtrOrPtrVectorTy() && |
| 2671 | DstTy->isPtrOrPtrVectorTy() && |
| 2672 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2673 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2674 | "Illegal addrspacecast, bitcast sequence!"); |
| 2675 | // Allowed, use first cast's opcode |
| 2676 | return firstOp; |
| 2677 | case 14: |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2678 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2679 | // bitcast's source is the same as that of addrspacecast's destination. |
Peter Collingbourne | 9d5fd4d | 2016-11-13 06:58:45 +0000 | [diff] [blame] | 2680 | if (SrcTy->getScalarType()->getPointerElementType() == |
| 2681 | DstTy->getScalarType()->getPointerElementType()) |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2682 | return Instruction::AddrSpaceCast; |
| 2683 | return 0; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2684 | case 15: |
| 2685 | // FIXME: this state can be merged with (1), but the following assert |
| 2686 | // is useful to check the correcteness of the sequence due to semantic |
| 2687 | // change of bitcast. |
| 2688 | assert( |
| 2689 | SrcTy->isIntOrIntVectorTy() && |
| 2690 | MidTy->isPtrOrPtrVectorTy() && |
| 2691 | DstTy->isPtrOrPtrVectorTy() && |
| 2692 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2693 | "Illegal inttoptr, bitcast sequence!"); |
| 2694 | // Allowed, use first cast's opcode |
| 2695 | return firstOp; |
| 2696 | case 16: |
| 2697 | // FIXME: this state can be merged with (2), but the following assert |
| 2698 | // is useful to check the correcteness of the sequence due to semantic |
| 2699 | // change of bitcast. |
| 2700 | assert( |
| 2701 | SrcTy->isPtrOrPtrVectorTy() && |
| 2702 | MidTy->isPtrOrPtrVectorTy() && |
| 2703 | DstTy->isIntOrIntVectorTy() && |
| 2704 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2705 | "Illegal bitcast, ptrtoint sequence!"); |
| 2706 | // Allowed, use second cast's opcode |
| 2707 | return secondOp; |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2708 | case 17: |
| 2709 | // (sitofp (zext x)) -> (uitofp x) |
| 2710 | return Instruction::UIToFP; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2711 | case 99: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2712 | // 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] | 2713 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2714 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2715 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2716 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2717 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2720 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2721 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2722 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2723 | // Construct and return the appropriate CastInst subclass |
| 2724 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2725 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2726 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2727 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2728 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2729 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2730 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2731 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2732 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2733 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2734 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2735 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2736 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2737 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2738 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2739 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2742 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2743 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2744 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2745 | // Construct and return the appropriate CastInst subclass |
| 2746 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2747 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2748 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2749 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2750 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2751 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2752 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2753 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2754 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2755 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2756 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2757 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2758 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2759 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2760 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2761 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2764 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2765 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2766 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2767 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2768 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2769 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2772 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2773 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2774 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2775 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2776 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2777 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2780 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2781 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2782 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2783 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2784 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2785 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2788 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2789 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2790 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2791 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2792 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2793 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2796 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2797 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2798 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2799 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2800 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2801 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2804 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2805 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2806 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2807 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2808 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2809 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2812 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2813 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2814 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2815 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2816 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2817 | "Invalid cast"); |
| 2818 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2819 | assert((!Ty->isVectorTy() || |
| 2820 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2821 | "Invalid cast"); |
| 2822 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2823 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2824 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2825 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2826 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2829 | /// Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2830 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2831 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2832 | Instruction *InsertBefore) { |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2833 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2834 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2835 | "Invalid cast"); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2836 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2837 | assert((!Ty->isVectorTy() || |
| 2838 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2839 | "Invalid cast"); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2840 | |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2841 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2842 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2843 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2844 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2845 | } |
| 2846 | |
| 2847 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2848 | Value *S, Type *Ty, |
| 2849 | const Twine &Name, |
| 2850 | BasicBlock *InsertAtEnd) { |
| 2851 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2852 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2853 | |
| 2854 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2855 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2856 | |
| 2857 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2858 | } |
| 2859 | |
| 2860 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2861 | Value *S, Type *Ty, |
| 2862 | const Twine &Name, |
| 2863 | Instruction *InsertBefore) { |
| 2864 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2865 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2866 | |
| 2867 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2868 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2869 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2870 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2873 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2874 | const Twine &Name, |
| 2875 | Instruction *InsertBefore) { |
| 2876 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2877 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2878 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2879 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2880 | |
| 2881 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2882 | } |
| 2883 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2884 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2885 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2886 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2887 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2888 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2889 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2890 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2891 | Instruction::CastOps opcode = |
| 2892 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2893 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2894 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2895 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2898 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2899 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2900 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2901 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2902 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2903 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2904 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2905 | Instruction::CastOps opcode = |
| 2906 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2907 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2908 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2909 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2912 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2913 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2914 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2915 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2916 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2917 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2918 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2919 | Instruction::CastOps opcode = |
| 2920 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2921 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2922 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2925 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2926 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2927 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2928 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2929 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2930 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2931 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2932 | Instruction::CastOps opcode = |
| 2933 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2934 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2935 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2938 | // Check whether it is valid to call getCastOpcode for these types. |
| 2939 | // This routine must be kept in sync with getCastOpcode. |
| 2940 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2941 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2942 | return false; |
| 2943 | |
| 2944 | if (SrcTy == DestTy) |
| 2945 | return true; |
| 2946 | |
| 2947 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2948 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2949 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2950 | // An element by element cast. Valid if casting the elements is valid. |
| 2951 | SrcTy = SrcVecTy->getElementType(); |
| 2952 | DestTy = DestVecTy->getElementType(); |
| 2953 | } |
| 2954 | |
| 2955 | // Get the bit sizes, we'll need these |
| 2956 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2957 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2958 | |
| 2959 | // Run through the possibilities ... |
| 2960 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2961 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2962 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2963 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2964 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2965 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2966 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2967 | // Casting from something else |
| 2968 | return SrcTy->isPointerTy(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2969 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2970 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2971 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2972 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2973 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2974 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2975 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2976 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2977 | // Casting from something else |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2978 | return false; |
| 2979 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2980 | if (DestTy->isVectorTy()) // Casting to vector |
| 2981 | return DestBits == SrcBits; |
| 2982 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2983 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2984 | return true; |
| 2985 | return SrcTy->isIntegerTy(); // Casting from integral |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2986 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2987 | if (DestTy->isX86_MMXTy()) { |
| 2988 | if (SrcTy->isVectorTy()) |
| 2989 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2990 | return false; |
| 2991 | } // Casting to something else |
| 2992 | return false; |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2995 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2996 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2997 | return false; |
| 2998 | |
| 2999 | if (SrcTy == DestTy) |
| 3000 | return true; |
| 3001 | |
| 3002 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3003 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 3004 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 3005 | // An element by element cast. Valid if casting the elements is valid. |
| 3006 | SrcTy = SrcVecTy->getElementType(); |
| 3007 | DestTy = DestVecTy->getElementType(); |
| 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 3013 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 3014 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 3019 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 3020 | |
| 3021 | // Could still have vectors of pointers if the number of elements doesn't |
| 3022 | // match |
| 3023 | if (SrcBits == 0 || DestBits == 0) |
| 3024 | return false; |
| 3025 | |
| 3026 | if (SrcBits != DestBits) |
| 3027 | return false; |
| 3028 | |
| 3029 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 3030 | return false; |
| 3031 | |
| 3032 | return true; |
| 3033 | } |
| 3034 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 3035 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3036 | const DataLayout &DL) { |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 3037 | // ptrtoint and inttoptr are not allowed on non-integral pointers |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 3038 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 3039 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 3040 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 3041 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 3042 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 3043 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 3044 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 3045 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 3046 | |
| 3047 | return isBitCastable(SrcTy, DestTy); |
| 3048 | } |
| 3049 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 3050 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 3051 | // 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] | 3052 | // logic in the castIsValid function below. This axiom should hold: |
| 3053 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 3054 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3055 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 3056 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3057 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3058 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3059 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 3060 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3061 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 3062 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 3063 | "Only first class types are castable!"); |
| 3064 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3065 | if (SrcTy == DestTy) |
| 3066 | return BitCast; |
| 3067 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 3068 | // FIXME: Check address space sizes here |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3069 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 3070 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3071 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 3072 | // An element by element cast. Find the appropriate opcode based on the |
| 3073 | // element types. |
| 3074 | SrcTy = SrcVecTy->getElementType(); |
| 3075 | DestTy = DestVecTy->getElementType(); |
| 3076 | } |
| 3077 | |
| 3078 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3079 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 3080 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3081 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3082 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3083 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 3084 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3085 | if (DestBits < SrcBits) |
| 3086 | return Trunc; // int -> smaller int |
| 3087 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3088 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3089 | return SExt; // signed -> SEXT |
| 3090 | else |
| 3091 | return ZExt; // unsigned -> ZEXT |
| 3092 | } else { |
| 3093 | return BitCast; // Same size, No-op cast |
| 3094 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3095 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3096 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3097 | return FPToSI; // FP -> sint |
| 3098 | else |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3099 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3100 | } else if (SrcTy->isVectorTy()) { |
| 3101 | assert(DestBits == SrcBits && |
| 3102 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3103 | return BitCast; // Same size, no-op cast |
| 3104 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3105 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3106 | "Casting from a value that is not first-class type"); |
| 3107 | return PtrToInt; // ptr -> int |
| 3108 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3109 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 3110 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3111 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3112 | return SIToFP; // sint -> FP |
| 3113 | else |
| 3114 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3115 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3116 | if (DestBits < SrcBits) { |
| 3117 | return FPTrunc; // FP -> smaller FP |
| 3118 | } else if (DestBits > SrcBits) { |
| 3119 | return FPExt; // FP -> larger FP |
| 3120 | } else { |
| 3121 | return BitCast; // same size, no-op cast |
| 3122 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3123 | } else if (SrcTy->isVectorTy()) { |
| 3124 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 3125 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 3126 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3127 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3128 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3129 | } else if (DestTy->isVectorTy()) { |
| 3130 | assert(DestBits == SrcBits && |
| 3131 | "Illegal cast to vector (wrong type or size)"); |
| 3132 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3133 | } else if (DestTy->isPointerTy()) { |
| 3134 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3135 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 3136 | return AddrSpaceCast; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3137 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3138 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3139 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3140 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3141 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3142 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3143 | if (SrcTy->isVectorTy()) { |
| 3144 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3145 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3146 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3147 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3148 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3149 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | //===----------------------------------------------------------------------===// |
| 3153 | // CastInst SubClass Constructors |
| 3154 | //===----------------------------------------------------------------------===// |
| 3155 | |
| 3156 | /// Check that the construction parameters for a CastInst are correct. This |
| 3157 | /// could be broken out into the separate constructors but it is useful to have |
| 3158 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 3159 | /// of the types involved. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3160 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3161 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3162 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3163 | Type *SrcTy = S->getType(); |
Evan Cheng | 098d7b7 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 3164 | |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 3165 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 3166 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3167 | return false; |
| 3168 | |
| 3169 | // 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] | 3170 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 3171 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3172 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3173 | // If these are vector types, get the lengths of the vectors (using zero for |
| 3174 | // scalar types means that checking that vector lengths match also checks that |
| 3175 | // scalars are not being converted to vectors or vectors to scalars). |
| 3176 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 3177 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 3178 | unsigned DstLength = DstTy->isVectorTy() ? |
| 3179 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 3180 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3181 | // Switch on the opcode provided |
| 3182 | switch (op) { |
| 3183 | default: return false; // This is an input error |
| 3184 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3185 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3186 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3187 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3188 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3189 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3190 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3191 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3192 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3193 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3194 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3195 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3196 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3197 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3198 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3199 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3200 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3201 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3202 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3203 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3204 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3205 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3206 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3207 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3208 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3209 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3210 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3211 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3212 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3213 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3214 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3215 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3216 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3217 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3218 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3219 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3220 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3221 | case Instruction::BitCast: { |
| 3222 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3223 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3224 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3225 | // BitCast implies a no-op cast of type only. No bits change. |
| 3226 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3227 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3228 | return false; |
| 3229 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3230 | // 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] | 3231 | // widths are identical. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3232 | if (!SrcPtrTy) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3233 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 3234 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3235 | // If both are pointers then the address spaces must match. |
| 3236 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 3237 | return false; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3238 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3239 | // A vector of pointers must have the same number of elements. |
Serguei Katkov | 09ab506 | 2018-08-21 04:27:07 +0000 | [diff] [blame] | 3240 | VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy); |
| 3241 | VectorType *DstVecTy = dyn_cast<VectorType>(DstTy); |
| 3242 | if (SrcVecTy && DstVecTy) |
| 3243 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3244 | if (SrcVecTy) |
| 3245 | return SrcVecTy->getNumElements() == 1; |
| 3246 | if (DstVecTy) |
| 3247 | return DstVecTy->getNumElements() == 1; |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3248 | |
| 3249 | return true; |
| 3250 | } |
| 3251 | case Instruction::AddrSpaceCast: { |
| 3252 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3253 | if (!SrcPtrTy) |
| 3254 | return false; |
| 3255 | |
| 3256 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3257 | if (!DstPtrTy) |
| 3258 | return false; |
| 3259 | |
| 3260 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 3261 | return false; |
| 3262 | |
| 3263 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3264 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3265 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3266 | |
| 3267 | return false; |
| 3268 | } |
| 3269 | |
| 3270 | return true; |
| 3271 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3276 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3277 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3278 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3282 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3283 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3284 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3288 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3289 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3290 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3294 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3295 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3296 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3297 | } |
| 3298 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3299 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3300 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3301 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 3304 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3305 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3306 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3307 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3311 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3312 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3313 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3317 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3318 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3319 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3323 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3324 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3325 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3329 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3330 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3331 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3335 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3336 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3337 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3341 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3342 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3343 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3347 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3348 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3349 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3353 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3354 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3355 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3359 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3360 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3361 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3365 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3366 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3367 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3371 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3372 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3373 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3377 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3378 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3379 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3383 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3384 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3385 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3389 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3390 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3391 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3395 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3396 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3397 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3401 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3402 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3403 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3407 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3408 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3409 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3413 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3414 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3415 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3416 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3417 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3418 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3419 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3420 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3421 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3422 | } |
| 3423 | |
| 3424 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3425 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3426 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3427 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3428 | } |
| 3429 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3430 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3431 | // CmpInst Classes |
| 3432 | //===----------------------------------------------------------------------===// |
| 3433 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3434 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3435 | Value *RHS, const Twine &Name, Instruction *InsertBefore, |
| 3436 | Instruction *FlagsSource) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3437 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3438 | OperandTraits<CmpInst>::op_begin(this), |
| 3439 | OperandTraits<CmpInst>::operands(this), |
| 3440 | InsertBefore) { |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3441 | Op<0>() = LHS; |
| 3442 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3443 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3444 | setName(Name); |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3445 | if (FlagsSource) |
| 3446 | copyIRFlags(FlagsSource); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3447 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3448 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3449 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3450 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3451 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3452 | OperandTraits<CmpInst>::op_begin(this), |
| 3453 | OperandTraits<CmpInst>::operands(this), |
| 3454 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3455 | Op<0>() = LHS; |
| 3456 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3457 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3458 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3462 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3463 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3464 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3465 | if (InsertBefore) |
| 3466 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3467 | S1, S2, Name); |
| 3468 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3469 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3470 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3471 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3472 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3473 | if (InsertBefore) |
| 3474 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3475 | S1, S2, Name); |
| 3476 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3477 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3478 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3482 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3483 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3484 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3485 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3486 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3487 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3488 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3489 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | void CmpInst::swapOperands() { |
| 3493 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3494 | IC->swapOperands(); |
| 3495 | else |
| 3496 | cast<FCmpInst>(this)->swapOperands(); |
| 3497 | } |
| 3498 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3499 | bool CmpInst::isCommutative() const { |
| 3500 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3501 | return IC->isCommutative(); |
| 3502 | return cast<FCmpInst>(this)->isCommutative(); |
| 3503 | } |
| 3504 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3505 | bool CmpInst::isEquality() const { |
| 3506 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3507 | return IC->isEquality(); |
| 3508 | return cast<FCmpInst>(this)->isEquality(); |
| 3509 | } |
| 3510 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3511 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3512 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3513 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3514 | case ICMP_EQ: return ICMP_NE; |
| 3515 | case ICMP_NE: return ICMP_EQ; |
| 3516 | case ICMP_UGT: return ICMP_ULE; |
| 3517 | case ICMP_ULT: return ICMP_UGE; |
| 3518 | case ICMP_UGE: return ICMP_ULT; |
| 3519 | case ICMP_ULE: return ICMP_UGT; |
| 3520 | case ICMP_SGT: return ICMP_SLE; |
| 3521 | case ICMP_SLT: return ICMP_SGE; |
| 3522 | case ICMP_SGE: return ICMP_SLT; |
| 3523 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3524 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3525 | case FCMP_OEQ: return FCMP_UNE; |
| 3526 | case FCMP_ONE: return FCMP_UEQ; |
| 3527 | case FCMP_OGT: return FCMP_ULE; |
| 3528 | case FCMP_OLT: return FCMP_UGE; |
| 3529 | case FCMP_OGE: return FCMP_ULT; |
| 3530 | case FCMP_OLE: return FCMP_UGT; |
| 3531 | case FCMP_UEQ: return FCMP_ONE; |
| 3532 | case FCMP_UNE: return FCMP_OEQ; |
| 3533 | case FCMP_UGT: return FCMP_OLE; |
| 3534 | case FCMP_ULT: return FCMP_OGE; |
| 3535 | case FCMP_UGE: return FCMP_OLT; |
| 3536 | case FCMP_ULE: return FCMP_OGT; |
| 3537 | case FCMP_ORD: return FCMP_UNO; |
| 3538 | case FCMP_UNO: return FCMP_ORD; |
| 3539 | case FCMP_TRUE: return FCMP_FALSE; |
| 3540 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3541 | } |
| 3542 | } |
| 3543 | |
Tim Northover | de3aea041 | 2016-08-17 20:25:25 +0000 | [diff] [blame] | 3544 | StringRef CmpInst::getPredicateName(Predicate Pred) { |
| 3545 | switch (Pred) { |
| 3546 | default: return "unknown"; |
| 3547 | case FCmpInst::FCMP_FALSE: return "false"; |
| 3548 | case FCmpInst::FCMP_OEQ: return "oeq"; |
| 3549 | case FCmpInst::FCMP_OGT: return "ogt"; |
| 3550 | case FCmpInst::FCMP_OGE: return "oge"; |
| 3551 | case FCmpInst::FCMP_OLT: return "olt"; |
| 3552 | case FCmpInst::FCMP_OLE: return "ole"; |
| 3553 | case FCmpInst::FCMP_ONE: return "one"; |
| 3554 | case FCmpInst::FCMP_ORD: return "ord"; |
| 3555 | case FCmpInst::FCMP_UNO: return "uno"; |
| 3556 | case FCmpInst::FCMP_UEQ: return "ueq"; |
| 3557 | case FCmpInst::FCMP_UGT: return "ugt"; |
| 3558 | case FCmpInst::FCMP_UGE: return "uge"; |
| 3559 | case FCmpInst::FCMP_ULT: return "ult"; |
| 3560 | case FCmpInst::FCMP_ULE: return "ule"; |
| 3561 | case FCmpInst::FCMP_UNE: return "une"; |
| 3562 | case FCmpInst::FCMP_TRUE: return "true"; |
| 3563 | case ICmpInst::ICMP_EQ: return "eq"; |
| 3564 | case ICmpInst::ICMP_NE: return "ne"; |
| 3565 | case ICmpInst::ICMP_SGT: return "sgt"; |
| 3566 | case ICmpInst::ICMP_SGE: return "sge"; |
| 3567 | case ICmpInst::ICMP_SLT: return "slt"; |
| 3568 | case ICmpInst::ICMP_SLE: return "sle"; |
| 3569 | case ICmpInst::ICMP_UGT: return "ugt"; |
| 3570 | case ICmpInst::ICMP_UGE: return "uge"; |
| 3571 | case ICmpInst::ICMP_ULT: return "ult"; |
| 3572 | case ICmpInst::ICMP_ULE: return "ule"; |
| 3573 | } |
| 3574 | } |
| 3575 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3576 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3577 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3578 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3579 | case ICMP_EQ: case ICMP_NE: |
| 3580 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3581 | return pred; |
| 3582 | case ICMP_UGT: return ICMP_SGT; |
| 3583 | case ICMP_ULT: return ICMP_SLT; |
| 3584 | case ICMP_UGE: return ICMP_SGE; |
| 3585 | case ICMP_ULE: return ICMP_SLE; |
| 3586 | } |
| 3587 | } |
| 3588 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3589 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3590 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3591 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3592 | case ICMP_EQ: case ICMP_NE: |
| 3593 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3594 | return pred; |
| 3595 | case ICMP_SGT: return ICMP_UGT; |
| 3596 | case ICMP_SLT: return ICMP_ULT; |
| 3597 | case ICMP_SGE: return ICMP_UGE; |
| 3598 | case ICMP_SLE: return ICMP_ULE; |
| 3599 | } |
| 3600 | } |
| 3601 | |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 3602 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { |
| 3603 | switch (pred) { |
| 3604 | default: llvm_unreachable("Unknown or unsupported cmp predicate!"); |
| 3605 | case ICMP_SGT: return ICMP_SGE; |
| 3606 | case ICMP_SLT: return ICMP_SLE; |
| 3607 | case ICMP_SGE: return ICMP_SGT; |
| 3608 | case ICMP_SLE: return ICMP_SLT; |
| 3609 | case ICMP_UGT: return ICMP_UGE; |
| 3610 | case ICMP_ULT: return ICMP_ULE; |
| 3611 | case ICMP_UGE: return ICMP_UGT; |
| 3612 | case ICMP_ULE: return ICMP_ULT; |
| 3613 | |
| 3614 | case FCMP_OGT: return FCMP_OGE; |
| 3615 | case FCMP_OLT: return FCMP_OLE; |
| 3616 | case FCMP_OGE: return FCMP_OGT; |
| 3617 | case FCMP_OLE: return FCMP_OLT; |
| 3618 | case FCMP_UGT: return FCMP_UGE; |
| 3619 | case FCMP_ULT: return FCMP_ULE; |
| 3620 | case FCMP_UGE: return FCMP_UGT; |
| 3621 | case FCMP_ULE: return FCMP_ULT; |
| 3622 | } |
| 3623 | } |
| 3624 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3625 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3626 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3627 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3628 | case ICMP_EQ: case ICMP_NE: |
| 3629 | return pred; |
| 3630 | case ICMP_SGT: return ICMP_SLT; |
| 3631 | case ICMP_SLT: return ICMP_SGT; |
| 3632 | case ICMP_SGE: return ICMP_SLE; |
| 3633 | case ICMP_SLE: return ICMP_SGE; |
| 3634 | case ICMP_UGT: return ICMP_ULT; |
| 3635 | case ICMP_ULT: return ICMP_UGT; |
| 3636 | case ICMP_UGE: return ICMP_ULE; |
| 3637 | case ICMP_ULE: return ICMP_UGE; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3638 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3639 | case FCMP_FALSE: case FCMP_TRUE: |
| 3640 | case FCMP_OEQ: case FCMP_ONE: |
| 3641 | case FCMP_UEQ: case FCMP_UNE: |
| 3642 | case FCMP_ORD: case FCMP_UNO: |
| 3643 | return pred; |
| 3644 | case FCMP_OGT: return FCMP_OLT; |
| 3645 | case FCMP_OLT: return FCMP_OGT; |
| 3646 | case FCMP_OGE: return FCMP_OLE; |
| 3647 | case FCMP_OLE: return FCMP_OGE; |
| 3648 | case FCMP_UGT: return FCMP_ULT; |
| 3649 | case FCMP_ULT: return FCMP_UGT; |
| 3650 | case FCMP_UGE: return FCMP_ULE; |
| 3651 | case FCMP_ULE: return FCMP_UGE; |
| 3652 | } |
| 3653 | } |
| 3654 | |
Max Kazantsev | b299ade | 2018-02-07 11:16:29 +0000 | [diff] [blame] | 3655 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { |
| 3656 | switch (pred) { |
| 3657 | case ICMP_SGT: return ICMP_SGE; |
| 3658 | case ICMP_SLT: return ICMP_SLE; |
| 3659 | case ICMP_UGT: return ICMP_UGE; |
| 3660 | case ICMP_ULT: return ICMP_ULE; |
| 3661 | case FCMP_OGT: return FCMP_OGE; |
| 3662 | case FCMP_OLT: return FCMP_OLE; |
| 3663 | case FCMP_UGT: return FCMP_UGE; |
| 3664 | case FCMP_ULT: return FCMP_ULE; |
| 3665 | default: return pred; |
| 3666 | } |
| 3667 | } |
| 3668 | |
Sanjoy Das | 6e78b17 | 2015-10-22 19:57:34 +0000 | [diff] [blame] | 3669 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { |
| 3670 | assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!"); |
| 3671 | |
| 3672 | switch (pred) { |
| 3673 | default: |
| 3674 | llvm_unreachable("Unknown predicate!"); |
| 3675 | case CmpInst::ICMP_ULT: |
| 3676 | return CmpInst::ICMP_SLT; |
| 3677 | case CmpInst::ICMP_ULE: |
| 3678 | return CmpInst::ICMP_SLE; |
| 3679 | case CmpInst::ICMP_UGT: |
| 3680 | return CmpInst::ICMP_SGT; |
| 3681 | case CmpInst::ICMP_UGE: |
| 3682 | return CmpInst::ICMP_SGE; |
| 3683 | } |
| 3684 | } |
| 3685 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3686 | bool CmpInst::isUnsigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3687 | switch (predicate) { |
| 3688 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3689 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3690 | case ICmpInst::ICMP_UGE: return true; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3694 | bool CmpInst::isSigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3695 | switch (predicate) { |
| 3696 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3697 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3698 | case ICmpInst::ICMP_SGE: return true; |
| 3699 | } |
| 3700 | } |
| 3701 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3702 | bool CmpInst::isOrdered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3703 | switch (predicate) { |
| 3704 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3705 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3706 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3707 | case FCmpInst::FCMP_ORD: return true; |
| 3708 | } |
| 3709 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3710 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3711 | bool CmpInst::isUnordered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3712 | switch (predicate) { |
| 3713 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3714 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3715 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3716 | case FCmpInst::FCMP_UNO: return true; |
| 3717 | } |
| 3718 | } |
| 3719 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3720 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3721 | switch(predicate) { |
| 3722 | default: return false; |
| 3723 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3724 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3725 | } |
| 3726 | } |
| 3727 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3728 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3729 | switch(predicate) { |
| 3730 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3731 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3732 | default: return false; |
| 3733 | } |
| 3734 | } |
| 3735 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3736 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3737 | // If the predicates match, then we know the first condition implies the |
| 3738 | // second is true. |
| 3739 | if (Pred1 == Pred2) |
| 3740 | return true; |
| 3741 | |
| 3742 | switch (Pred1) { |
| 3743 | default: |
| 3744 | break; |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3745 | case ICMP_EQ: |
Chad Rosier | 3d75f8c | 2016-04-25 13:25:14 +0000 | [diff] [blame] | 3746 | // 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] | 3747 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || |
| 3748 | Pred2 == ICMP_SLE; |
Chad Rosier | 3456cb5 | 2016-04-22 17:14:12 +0000 | [diff] [blame] | 3749 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. |
| 3750 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; |
| 3751 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. |
| 3752 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; |
| 3753 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. |
| 3754 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; |
| 3755 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. |
| 3756 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3757 | } |
| 3758 | return false; |
| 3759 | } |
| 3760 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3761 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3762 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3763 | } |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3764 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3765 | //===----------------------------------------------------------------------===// |
| 3766 | // SwitchInst Implementation |
| 3767 | //===----------------------------------------------------------------------===// |
| 3768 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3769 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3770 | assert(Value && Default && NumReserved); |
| 3771 | ReservedSpace = NumReserved; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3772 | setNumHungOffUseOperands(2); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3773 | allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3774 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3775 | Op<0>() = Value; |
| 3776 | Op<1>() = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3779 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3780 | /// switch on and a default destination. The number of additional cases can |
| 3781 | /// be specified here to make memory allocation more efficient. This |
| 3782 | /// constructor can also autoinsert before another instruction. |
| 3783 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3784 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3785 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3786 | nullptr, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3787 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3791 | /// switch on and a default destination. The number of additional cases can |
| 3792 | /// be specified here to make memory allocation more efficient. This |
| 3793 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3794 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3795 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3796 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3797 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3798 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3801 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3802 | : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3803 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3804 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3805 | Use *OL = getOperandList(); |
| 3806 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3807 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3808 | OL[i] = InOL[i]; |
| 3809 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3810 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3811 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | /// addCase - Add an entry to the switch instruction... |
| 3815 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3816 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3817 | unsigned NewCaseIdx = getNumCases(); |
| 3818 | unsigned OpNo = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3819 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3820 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3821 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3822 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3823 | setNumHungOffUseOperands(OpNo+2); |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3824 | CaseHandle Case(this, NewCaseIdx); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3825 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3826 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3829 | /// removeCase - This method removes the specified case and its successor |
| 3830 | /// from the switch instruction. |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3831 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { |
| 3832 | unsigned idx = I->getCaseIndex(); |
| 3833 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3834 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3835 | |
| 3836 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3837 | Use *OL = getOperandList(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3838 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3839 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3840 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3841 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3842 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3846 | OL[NumOps-2].set(nullptr); |
| 3847 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3848 | setNumHungOffUseOperands(NumOps-2); |
Chandler Carruth | 0d256c0 | 2017-03-26 02:49:23 +0000 | [diff] [blame] | 3849 | |
| 3850 | return CaseIt(this, idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3853 | /// growOperands - grow operands - This grows the operand list in response |
| 3854 | /// 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] | 3855 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3856 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3857 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3858 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3859 | |
| 3860 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3861 | growHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3864 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3865 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3866 | //===----------------------------------------------------------------------===// |
| 3867 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3868 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3869 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3870 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3871 | ReservedSpace = 1+NumDests; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3872 | setNumHungOffUseOperands(1); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3873 | allocHungoffUses(ReservedSpace); |
| 3874 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3875 | Op<0>() = Address; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3879 | /// growOperands - grow operands - This grows the operand list in response |
| 3880 | /// 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] | 3881 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3882 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3883 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3884 | unsigned NumOps = e*2; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3886 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3887 | growHungoffUses(ReservedSpace); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3890 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3891 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3892 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3893 | Instruction::IndirectBr, nullptr, 0, InsertBefore) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3894 | init(Address, NumCases); |
| 3895 | } |
| 3896 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3897 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3898 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3899 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3900 | Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3901 | init(Address, NumCases); |
| 3902 | } |
| 3903 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3904 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3905 | : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3906 | nullptr, IBI.getNumOperands()) { |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3907 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3908 | Use *OL = getOperandList(); |
| 3909 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3910 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3911 | OL[i] = InOL[i]; |
| 3912 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3913 | } |
| 3914 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3915 | /// addDestination - Add a destination. |
| 3916 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3917 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3918 | unsigned OpNo = getNumOperands(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3919 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3920 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3921 | // Initialize some new operands. |
| 3922 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3923 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3924 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3928 | /// indirectbr instruction. |
| 3929 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3930 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3931 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3932 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3933 | Use *OL = getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3934 | |
| 3935 | // Replace this value with the last one. |
| 3936 | OL[idx+1] = OL[NumOps-1]; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3937 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3938 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3939 | OL[NumOps-1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3940 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3943 | //===----------------------------------------------------------------------===// |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3944 | // cloneImpl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3945 | //===----------------------------------------------------------------------===// |
| 3946 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3947 | // Define these methods here so vtables don't get emitted into every translation |
| 3948 | // unit that uses these classes. |
| 3949 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3950 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3951 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Cameron McInally | cbde0d9 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 3954 | UnaryOperator *UnaryOperator::cloneImpl() const { |
| 3955 | return Create(getOpcode(), Op<0>()); |
| 3956 | } |
| 3957 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3958 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3959 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3962 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3963 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3966 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3967 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3970 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3971 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3974 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3975 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3978 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3979 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 3980 | getType()->getAddressSpace(), |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3981 | (Value *)getOperand(0), getAlignment()); |
| 3982 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 3983 | Result->setSwiftError(isSwiftError()); |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3984 | return Result; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3987 | LoadInst *LoadInst::cloneImpl() const { |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 3988 | return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3989 | getAlignment(), getOrdering(), getSyncScopeID()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3992 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3993 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3994 | getAlignment(), getOrdering(), getSyncScopeID()); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3995 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3998 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3999 | AtomicCmpXchgInst *Result = |
| 4000 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4001 | getSuccessOrdering(), getFailureOrdering(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 4002 | getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4003 | Result->setVolatile(isVolatile()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4004 | Result->setWeak(isWeak()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4005 | return Result; |
| 4006 | } |
| 4007 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4008 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4009 | AtomicRMWInst *Result = |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 4010 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), |
| 4011 | getOrdering(), getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4012 | Result->setVolatile(isVolatile()); |
| 4013 | return Result; |
| 4014 | } |
| 4015 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4016 | FenceInst *FenceInst::cloneImpl() const { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 4017 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4020 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4021 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4024 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4025 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4028 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4029 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4032 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4033 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4036 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4037 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4040 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4041 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4044 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4045 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4048 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4049 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4052 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4053 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4056 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4057 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4060 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4061 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4062 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4063 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4064 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4065 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4066 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4067 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4068 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 4069 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 4070 | } |
| 4071 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4072 | CallInst *CallInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 4073 | if (hasOperandBundles()) { |
| 4074 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 4075 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); |
| 4076 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4077 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4080 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4081 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4082 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4083 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4084 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4085 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4086 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4087 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4088 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4089 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4090 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4091 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4092 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 4093 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4096 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 4097 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4098 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4099 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4100 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4101 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4102 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4103 | return new LandingPadInst(*this); |
| 4104 | } |
| 4105 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4106 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4107 | return new(getNumOperands()) ReturnInst(*this); |
| 4108 | } |
| 4109 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4110 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 4111 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4112 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4113 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4114 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4115 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4116 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4117 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4120 | InvokeInst *InvokeInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 4121 | if (hasOperandBundles()) { |
| 4122 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 4123 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); |
| 4124 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4125 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4126 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4127 | |
Craig Topper | 784929d | 2019-02-08 20:48:56 +0000 | [diff] [blame] | 4128 | CallBrInst *CallBrInst::cloneImpl() const { |
| 4129 | if (hasOperandBundles()) { |
| 4130 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 4131 | return new (getNumOperands(), DescriptorBytes) CallBrInst(*this); |
| 4132 | } |
| 4133 | return new (getNumOperands()) CallBrInst(*this); |
| 4134 | } |
| 4135 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4136 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4137 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4138 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 4139 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 4140 | } |
| 4141 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4142 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 4143 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4146 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { |
| 4147 | return new CatchSwitchInst(*this); |
| 4148 | } |
| 4149 | |
| 4150 | FuncletPadInst *FuncletPadInst::cloneImpl() const { |
| 4151 | return new (getNumOperands()) FuncletPadInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4154 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 4155 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4156 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4157 | } |