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