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